如果是单体项目,可以考虑使用Ehcache缓存,和redis相比,不需要部署,也节约服务器资源
配置类
@Component
public class EhcacheConfig {
/**
* 获取缓存管理器
*
* @return CacheManager
*/
@Bean
public CacheManager getCacheManager() {
// 本项目的cache
CacheConfiguration config = new CacheConfiguration();
config.setName(BasicConstant.PROJECT_NAME);
//是否永不过期,为false则过期需要通过timeToIdleSeconds,timeToLiveSeconds判断
config.setEternal(false);
//最少使用
config.setMemoryStoreEvictionPolicy("LFU");
//内存中存放的最大记录数
config.setMaxBytesLocalHeap("1G");
config.setMaxBytesLocalDisk("1G");
// config.setMaxEntriesLocalHeap(10000);
// config.setMaxEntriesLocalDisk(20000);
// config.setClearOnFlush(true);
//内存中过多则存入硬盘
config.setOverflowToDisk(false);
//重启服务后是否恢复缓存
config.setDiskPersistent(false);
// 设置ehcache配置文件,获取CacheManager
Configuration configuration = new Configuration();
configuration.addCache(config);
// 将CacheManager注册为bean,供缓存工具类使用
return CacheManager.newInstance(configuration);
}
}
值得注意的是:
上方三个方法是设置内存中可存放的最大缓存条数
config.setMaxEntriesLocalHeap(10000); 内存中存放的最大记录数
config.setMaxEntriesLocalDisk(20000); 硬盘中存放的最大记录数
config.setClearOnFlush(true); 超过记录数自动删除
但是如果我们的一条缓存的数据量很大,而且设置的内存最大存放记录数也很大,可能会出现记录数没达到,但是内存被占满了,从而导致内存溢出
Ehcache缓存工具类
@Component
public class EhcacheUtil {
@Lazy
private final CacheManager cacheManager;
/**
* key
*/
public static final String PROJECT_NAME="baseKey-auth";
/**
* 默认的缓存存在时间(秒)
*/
private static final int DEFAULT_LIVE_SECOND = 60 * 60 * 12;
private static final int DEFAULT_TIME_IDLE_SECONDS = 60 * 60 * 8;
public EhcacheUtil(CacheManager cacheManager) {
this.cacheManager = cacheManager;
}
/**
* 添加缓存
*
* @param key
* @param value
* @param timeToLiveSeconds 缓存生存时间(秒)
*/
public void set(String key, String value, int timeToLiveSeconds) {
Cache cache = cacheManager.getCache(PROJECT_NAME);
Element element = new Element(
key, value,
// timeToIdleSeconds=0
0,
timeToLiveSeconds);
cache.put(element);
}
/**
* 添加缓存
* 使用默认生存时间,默认存活时间
*
* @param key
* @param value
*/
public void set(String key, String value) {
Cache cache = cacheManager.getCache(PROJECT_NAME);
Element element = new Element(
key, value,
DEFAULT_TIME_IDLE_SECONDS,
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(PROJECT_NAME);
Element element = new Element(
key, value,
timeToIdleSeconds,
timeToLiveSeconds);
cache.put(element);
}
/**
* 获取缓存
*
* @param key
* @return
*/
public String get(String key) {
Cache cache = cacheManager.getCache(PROJECT_NAME);
Element element = cache.get(key);
if (element == null) {
return null;
}
return (String) element.getObjectValue();
}
/**
* 删除缓存
*
* @param key
* @return
*/
public void delete(String key) {
Cache cache = cacheManager.getCache(PROJECT_NAME);
Element element = cache.get(key);
if (element == null) {
return;
}
cache.remove(key);
}
}