springboot缓存-Ehcache

一、Maven

<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.8.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>

二、ehcache.xml 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false">
<diskStore path="java.io.tmpdir/Tmp_EhCache"/>

<!-- defaultCache,是默认的缓存策略 -->
<!-- 如果你指定的缓存策略没有找到,那么就用这个默认的缓存策略 -->
<!-- external:缓存对象是否一直存在,如果设置为true的话,那么timeout就没有效果,缓存就会一直存在,一般默认就是false -->
<!-- maxElementsInMemory:内存中可以缓存多少个缓存条目 -->
<!-- overflowToDisk:如果内存不够的时候,是否溢出到磁盘 -->
<!-- diskPersistent:是否启用磁盘持久化的机制,在jvm崩溃的时候和重启之间 -->
<!-- timeToIdleSeconds:对象最大的闲置的时间,如果超出闲置的时间,可能就会过期 单位:秒 当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大-->
<!-- timeToLiveSeconds:对象最多存活的时间 单位:秒 当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是存活时间无穷大-->
<!-- memoryStoreEvictionPolicy:当缓存数量达到了最大的指定条目数的时候,需要采用一定的算法,从缓存中清除一批数据,LRU,最近最少使用算法,最近一段时间内,最少使用的那些数据,就被干掉了 -->
<defaultCache
eternal="false"
maxElementsInMemory="1000"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="300"
timeToLiveSeconds="0"
memoryStoreEvictionPolicy="LRU"/>

<!-- 手动指定的缓存策略 -->
<!-- 对不同的数据,缓存策略可以在这里配置多种 -->
<cache
name="local"
eternal="false"
maxElementsInMemory="1000"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="300"
timeToLiveSeconds="0"
memoryStoreEvictionPolicy="LRU"/>
</ehcache>

三、application.yml中添加ehcache
spring:
cache:
type: ehcache
ehcache:
config: classpath:/config/ehcache.xml

四、使用
1.工具类方式
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

import java.io.BufferedInputStream;
import java.io.InputStream;

public class CacheUtil {

public void setCacheList(String key, Object value) {
InputStream inputStream = new BufferedInputStream(this.getClass().getResourceAsStream("/config/ehcache.xml"));
CacheManager create = CacheManager.create(inputStream);
Cache cache = create.getCache("local");
cache.put(new Element(key, value));
}

public Object getCacheList(String key) {
InputStream inputStream = new BufferedInputStream(this.getClass().getResourceAsStream("/config/ehcache.xml"));
CacheManager create = CacheManager.create(inputStream);
Cache cache = create.getCache("local");
Element element = cache.get(key);
if (element==null){
return null;
}
return element.getObjectValue();
}
}

具体方法中:
先检查缓存中是否存在,如果存在直接返回结果,不存在,先查询,保存到缓存中,在返回结果
CacheUtil cacheUtil = new CacheUtil();
Object object = cacheUtil.getCacheList(params.toJSONString());
if (object != null) {
return object;
}
....  //省略查询业务代码
cacheUtil.setCacheList(params.toJSONString(), pageInfo);

2.springboot注解使用方式
首先添加配置
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

@Configuration
@EnableCaching
public class EhcacheConfiguration {
@Bean
public EhCacheManagerFactoryBean cacheManagerFactoryBean() {
EhCacheManagerFactoryBean bean = new EhCacheManagerFactoryBean();
bean.setConfigLocation(new ClassPathResource("config/ehcache.xml"));
bean.setShared(true);
return bean;
}

@Bean
public EhCacheCacheManager ehCacheCacheManager(EhCacheManagerFactoryBean bean) {
return new EhCacheCacheManager(bean.getObject());
}
}

接着在使用的方法上添加注解
@Cacheable(cacheNames = {"local"}) //cacheNames 是 ehcache.xml文件中定义的配置(不指定则采用默认的配置),缓存的结果已key/value的形式保存,spring提供了默认的key生成规则。


 

 




posted @ 2021-06-11 16:21  guohf  阅读(195)  评论(0编辑  收藏  举报