SpringBoot集成EhCache
2019-02-20 本文已影响1人
后端技术学习分享
目标
网上的集成方案基本都是使用缓存注解来使用ehcache,但在实际应用中很不灵活。本文介绍了非注解方式无配置文件的集成方案。适用于小项目。
缓存思路
- 只配置一个cache,项目里所有缓存都存这里。
- 不使用spring自带的缓存注解
- 缓存value均使用string,实体类等通过JSON工具转为json字符串再缓存
Ehcache Config代码
EhcacheConfig.java
主要是根据配置文件,获取CacheManager,供缓存工具类使用
import com.spz.demo.singleboot.common.CacheConst;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import net.sf.ehcache.config.CacheConfiguration;
import net.sf.ehcache.config.Configuration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;
import java.io.FileNotFoundException;
import java.net.URL;
@Component
public class EhcacheConfig {
/**
* 获取缓存管理器
* @return
*/
@Bean
public CacheManager getCacheManager(){
// 本项目的cache
CacheConfiguration config = new CacheConfiguration();
config.setName(CacheConst.PROJECT);
config.setEternal(false);//是否永不过期,为false则过期需要通过timeToIdleSeconds,timeToLiveSeconds判断
config.setMemoryStoreEvictionPolicy("LFU");//最少使用
config.setMaxElementsInMemory(10000);//内存中存放的最大记录数
config.setMaxElementsOnDisk(20000);
config.setOverflowToDisk(true);//内存中过多则存入硬盘
config.setDiskPersistent(false);//重启服务后是否恢复缓存
// 设置ehcache配置文件,获取CacheManager
Configuration configuration = new Configuration();
configuration.addCache(config);
CacheManager cacheManager = CacheManager.newInstance(configuration);
// 将CacheManager注册为bean,供缓存工具类使用
return cacheManager;
}
}
缓存工具类
EhcacheUtil.java
import com.spz.demo.singleboot.common.CacheConst;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* EhCache 缓存工具类
* 使用String格式的value
*
* {@link com.spz.demo.singleboot.config.EhcacheConfig}
* {@link CacheConst}
* @author zp
*/
@Component
public class EhcacheUtil {
@Autowired
@Lazy
private CacheManager cacheManager;
// 默认的缓存存在时间(秒)
private static final int DEFAULT_LIVE_SECOND = 20 * 60;
/**
* 添加缓存
* @param key
* @param value
* @param timeToLiveSeconds 缓存生存时间(秒)
*/
public void set(String key,String value,int timeToLiveSeconds){
Cache cache = cacheManager.getCache(CacheConst.PROJECT);
Element element = new Element(
key, value,
0,// timeToIdleSeconds=0
timeToLiveSeconds);
cache.put(element);
}
/**
* 添加缓存
* 使用默认生存时间 {@link DEFAULT_LIVE_SECOND}
* @param key
* @param value
*/
public void set(String key,String value){
Cache cache = cacheManager.getCache(CacheConst.PROJECT);
Element element = new Element(
key, value,
0,// timeToIdleSeconds
DEFAULT_LIVE_SECOND);
cache.put(element);
}
/**
* 添加缓存
* @param key
* @param value
* @param timeToIdleSeconds 对象空闲时间,指对象在多长时间没有被访问就会失效。
* 只对eternal为false的有效。传入0,表示一直可以访问。以秒为单位。
* @param timeToLiveSeconds 缓存生存时间(秒)
* 只对eternal为false的有效
*/
public void set(String key,String value,int timeToIdleSeconds, int timeToLiveSeconds){
Cache cache = cacheManager.getCache(CacheConst.PROJECT);
Element element = new Element(
key, value,
timeToIdleSeconds,
timeToLiveSeconds);
cache.put(element);
}
/**
* 获取缓存
* @param key
* @return
*/
public String get(String key){
Cache cache = cacheManager.getCache(CacheConst.PROJECT);
Element element = cache.get(key);
if(element == null){
return null;
}
return (String) element.getObjectValue();
}
}
使用
将实体类转换为json字符串,再根据key存入缓存即可
参考
ehcache缓存配置与参数说明
关于Ehcache缓存中timeToLiveSeconds和timeToIdleSeconds