maven配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.10.0</version>
</dependency>
ehcache.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<config
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns='http://www.ehcache.org/v3'
xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core.xsd">
<!-- 缓存模版,此处为了显示其用法,也可以不用模版直接在cache中配置与模版参数相同 -->
<cache-template name="cache_template">
<!-- 设置缓存对象中key、value 的类型为String,如果没有指定则默认为java.lang.Object -->
<key-type>java.lang.String</key-type>
<value-type>java.lang.String</value-type>
<expiry>
<!-- 单位默认为秒当用秒作单位时,可以不填-->
<ttl unit="hours">1</ttl>
</expiry>
<resources>
<!--设置该缓存在堆上可持有2000个实体(键值对) -->
<heap unit="entries">500</heap>
<!--最大可持有500MB的堆外内存 -->
<offheap unit="MB">50</offheap>
</resources>
</cache-template>
<cache alias="web_code_cache" uses-template="cache_template">
<expiry>
<!--此处会覆盖模版中的(TTL)配置 -->
<tti>300</tti>
</expiry>
</cache>
<cache alias="recommend_articles" uses-template="cache_template">
<value-type>java.util.ArrayList</value-type>
<expiry>
<!--此处会覆盖模版中的(TTL)配置 -->
<tti unit="hours">12</tti>
</expiry>
</cache>
<cache alias="hot_articles" uses-template="cache_template">
<value-type>java.util.ArrayList</value-type>
<expiry>
<!--此处会覆盖模版中的(TTL)配置 -->
<tti unit="hours">12</tti>
</expiry>
</cache>
<cache alias="recently_articles" uses-template="cache_template">
<value-type>java.util.ArrayList</value-type>
<expiry>
<!--此处会覆盖模版中的(TTL)配置 -->
<tti unit="minutes">3</tti>
</expiry>
<resources>
<offheap unit="MB">30</offheap>
</resources>
</cache>
<cache alias="hot_tags" uses-template="cache_template">
<value-type>java.util.ArrayList</value-type>
<expiry>
<!--此处会覆盖模版中的(TTL)配置 -->
<tti unit="hours">12</tti>
</expiry>
<resources>
<offheap unit="MB">10</offheap>
</resources>
</cache>
</config>
cache类配置
/**
* 缓存配置
*/
@Configuration
@EnableCaching
public class CacheConfig {
}
资源配置
spring:
cache:
type: jcache
jcache:
provider: org.ehcache.jsr107.EhcacheCachingProvider
config: classpath:ehcache.xml
缓存使用
@Cacheable(value = "hot_tags", key = "#siteId.toString().concat('-').concat(#count.toString())")
@Override
public List<ArticleTag> listTags(Long siteId, Integer count) {
}
在代码中使用
public class CaptchaService {
@Autowired
private CacheManager cacheManager;
public boolean checkCaptcha(String code) {
final String codeKey = RequestUtils.getClientId(RequestUtils.getRequest());
Cache.ValueWrapper valueWrapper = cacheManager.getCache(WEB_CODE_CACHE_NAME).get(codeKey);
String codeInMemory = StrUtils.isBlank (codeKey) ? "" : (valueWrapper == null) ? "" : (String) valueWrapper.get();
if(StrUtils.isNotBlank(codeKey)) {
cacheManager.getCache(WEB_CODE_CACHE_NAME).evict(codeKey);
}
return StrUtils.isNotBlank(code) && code.equalsIgnoreCase(codeInMemory);
}
public FastcmsCaptcha getCaptcha() {
HttpServletRequest request = RequestUtils.getRequest();
SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 4);
final String verCode = specCaptcha.text().toLowerCase();
final String key = StrUtils.isEmpty(RequestUtils.getClientId(request)) ? StrUtils.uuid() : RequestUtils.getClientId(request);
cacheManager.getCache(WEB_CODE_CACHE_NAME).put(key, verCode);
return new FastcmsCaptcha(key, specCaptcha.toBase64());
}
}