目录
1、ehcache依赖的引入
2、ehcache配置文件的编写
3、ehcache辅助类的实现
4、使用ehcache的get / put方法
一、ehcache依赖
首先是添加依赖
<!-- ehcache 缓存 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.11</version>
</dependency>
然后开启缓存功能,在SpringBoot启动类上加入注解:@EnableCaching
二、ehcache配置文件
然后在resources下添加配置文件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">
<!--
| Please see http://ehcache.sourceforge.net/documentation/configuration.html for
| detailed information on how to configurigure caches in this file
+-->
<!-- Location of persistent caches on disk -->
<!-- 这个路径把你的项目名改下就可以了 -->
<diskStore path="/opt/app/你的项目名/cache" />
<!-- 这个是普通的设置,照这样就行了 -->
<defaultCache eternal="false" maxElementsInMemory="1000"
overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU"/>
<cache name="你的缓存名字,在初始化的时候会通过这个识别,如myCache" eternal="false"
maxElementsInMemory="1000" overflowToDisk="true" diskPersistent="false"
timeToIdleSeconds="360000" timeToLiveSeconds="360000"
memoryStoreEvictionPolicy="LRU" />
<!-- 如果有多个缓存,复制上面这段,修改一下缓存名字(如myCache2)就可以了 -->
</ehcache>
三、ehcache类
这一步主要是封装ehcache默认的get/put方法,使其符合我们实际工作中代码要求。所以我们新建一个包,可以命名为ehcache,在该包下新建类MyCache,内容如下
@Service(value = "myCache")//这个value可以随便命名
@Scope(value = "singleton")
public class MyCache implements InitializingBean {
@Autowired
private EhCacheCacheManager cacheManager;
private EhCacheCache cache;
/**
* 把你的实体类/对象(数据)存入缓存
*
* @param yourEntity
*/
public synchronized void putYourEntity (YourEntity newYourEntity ) {
YourEntity oldYourEntity = null;
// 缓存也是以key-value形式存取的,这里建议key为对象的主键,或者唯一索引的多字段
// 保证value唯一
ValueWrapper vw = cache.get(newYourEntity.getPrimaryKey());
if( vw != null) {
// 如果不为空,说明缓存已经存在对应的value了,那么就需要选择是否更新缓存
// 先获得旧的value
oldYourEntity = (YourEntity) vw.get();
// 是否满足更新条件
if( oldYourEntity.getTime().compareTo(newYourEntity.getTime())<0){
// 更新
cache.put(newYourEntity.getPrimaryKey(), newYourEntity );
}
// 不满足更新条件,不作任何操作
} else {
// 如果为空,说明第一次存入缓存,直接插入
cache.put(newYourEntity.getPrimaryKey(), newYourEntity );
}
}
/**
* 从缓存里面取出数据
* @param key
* @return the yourEntity
*/
public synchronized YourEntity getYourEntity(String PrimaryKey) {
YourEntity yourEntity = null;
ValueWrapper vw = cache.get(PrimaryKey);
if( vw != null) {
// 缓存里面存在对应的key,返回该value
yourEntity = (YourEntity ) vw.get();
// value已经取出,删除key
cache.evict(PrimaryKey);
}
//返回该value,如果该key在缓存没有对应的value,就会返回null
return yourEntity ;
}
/**
* 缓存里面存了多少个value
*
* @return the int
*/
public int showCacheSize() {
return cache.getNativeCache().getSize();
}
/**
* 初始化cache
*/
public void init() {
// 这个myCache就是你在ehcache.xml自定义的name值(缓存名字)
cache = (EhCacheCache) cacheManager.getCache("myCache");
}
@Override
public void afterPropertiesSet() throws Exception {
init();
}
}
然后你自动注入的EhCacheCacheManager在IDE下可能会标波浪线,
提示信息为:
Could not autowire. No beans of 'EhCacheCacheManager' type found. less... (Ctrl+F1)
Checks autowiring problems in a bean class.
这个是智能的IDE检测规则的问题,不影响你程序的任何执行。如果你看着实在不爽,把这个提示规则禁用就好了
解决方法一:IDE快捷提示键Alt+回车,Eclipse的我忘记了
解决方法二:如果你的快捷键跟我不一样,把里面的√去掉就好了
四、使用ehcache
在别的类注入ehcache类
@Autowired
private MyCache myCache;
然后调用ehcache类的putYourEntity/getYourEntity方法就可以了