- Spring Cache整合Redis
- Spring Cache整合Ehcache
- SpringCache整合Redis
创建SpringBoot项目,勾选
Web下面的Spring Web Starter依赖
NoSQL下面的Spring Data Redis(Access+Driver)依赖
Security下面的Spring Security依赖
I/O下面的Spring cache abstraction依赖
杂交
application.properties下配置Redis
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.database=0
spring.redis.password=123
spring.cache.cache-names=c1 //缓存名字
创建一个user实体类,get/set()---略
public class User implements Serializable { //实现序列化接口
private Integer id;
private String username;
private String address;
}
创建一个UserService
@Service
public class UserService {
//该方法具备缓存的功能,名字就是上面配置的缓存名
@Cacheable(cacheNames="c1",key="#id") //key="#id" 可以指定缓存的key,后面无论有多少个参数,只认定为一个缓存
public User getUserById(Integer id){
System.out.println(id);
User user = new User();
user.setId(id);
return user;
}
}
在启动类中加入@EnableCaching 注解,表示启动缓存
写一个测试
@Autowired
UserService userService;
@Test
public void test(){ //由于启动了缓存,控制台看到getUserById方法执行了一次,但会返回两次结果
User u1 = userService.getUserById(1);//前提是方法内的两个参数要一样,才会启动缓存
User u2 = userService.getUserById(1);
System.out.println(u1);
System.out.println(u2);
}
//一般提供的key够用了,如果想自定义key比较复杂的话,可以创建一个类implements KeyGenerator来自定义key
@Component
public class MyKeyGenerator implements KeyGenerator {
@Override
public Object generate(Object 0,Method method,Object...object) {
//随便定义
return method.getName()+":"+Arrays.toString(objects);
}
}
//@Cacheable(cacheNames="c1",KeyGenerator="myKeyGenerator")
@CacheEvict(cacheNames="c1") //当执行删除了数据库的id,同时也会把缓存里面的也删除
public void deleteUserById(Integer id){
System.out.println(id);
}
@CachePut(cacheNames="id" key="#user.id") //在更新数据库的同时,更新缓存里面的数据
public User updateUserById(User user) {
return user
}
//也可以使用@CacheConfig注解放在类名上
- Spring Cache整合Ehcache
创建Spring Boot项目
在pom文件中加入相关依赖
<depedency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.6</version>
</depedency>
在resources目录下新建一个ehcache.xml文件,建议最好使用这一个名字
//假如不是这个名字,可以在application.properties中指定
//spring.cache.ehcahce.config=classpath:aa.xml
<ehacahe>
<diskStore path="java.io.tmpdir/ehcahce"/>
<!--
name:缓存名称。
maxElementsInMemory:缓存最大个数。
eternal:对象是否永久有效,一旦设置了,timeout将不起作用。
timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。
仅当external=false对象不是永久有效时使用,可选属性,默认值是0,
也就是可闲置时间无穷大。
timeToLiveSeconds:设置对象在失效前允许存活时间。最大时间介于创建
时间和失效时间之间。仅当external=false对象不是永久有效时使用,默认
是0,也就是对象存活时间无穷大。
overflowToDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache
将对象写到磁盘中。
diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。
默认是30MB。每个Cache都应该有自己的一个缓冲区。
maxElementsOnDisk:硬盘最大缓存个数。
diskPersistent:是否缓存虚拟机重启数据 Where the disk store persists between
restarts of the Virtual Machine.The default value is false.
diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。
memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将
会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)你可以设置为FIFO
(先进先出)或者LFU(较少使用)。
clearOnFlush:内存数量最大时是否清除。)
-->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
/>
<cache name="mycache" //@Cacheable(cacheNames="mtcache")
maxElementsInMemory="10000"
eternal="true"
overflowToDisk="true"
diskPersistent="true"
diskExpiryThreadIntervalSeconds="600"
// <cache name="org.apache.shiro.realm.SimpleAccountRealm.authorization"
// maxElementsInMemory="100"
// eternal="false"
// timeToLiveSeconds="600"
// overflowToDisk="false"/>
</encache>
在启动类中加入@EnaleCaching注解即可
后面的操作同上 Spring Cache.
@Cacheable(cacheNames="mtcache")