基础介绍
@Cacheable、@CachePut、@CacheEvict是Spring Cache框架中常用的三个缓存注解,它们分别表示查询、更新和清除缓存。
- @Cacheable
@Cacheable注解可以标记方法,让方法的返回值被缓存起来,相同参数下下次调用该方法时直接从缓存中获取值,避免重复执行方法逻辑。该注解支持以下属性:
- value:指定缓存的名称,可以指定多个缓存名称,各缓存之间以逗号分隔。
- key:指定缓存的key,可以使用SpEL表达式,缺省表示使用方法的所有参数作为key。
- condition:指定缓存的条件,可以使用SpEL表达式,只有条件满足时,才缓存方法的返回值。例如:@Cacheable(value="mycache", key="#id", condition="#id > 0")。
- unless:指定缓存的条件,表示只有当unless表达式的值为false时,才缓存方法的返回值。例如:@Cacheable(value="mycache", key="#id", unless="#result == null")。
- @CachePut
@CachePut注解可以标记方法,让方法的返回值被更新或者缓存起来,常用于更新缓存中的数据。该注解支持以下属性:
- value:指定缓存的名称,与@Cacheable注解相同。
- key:指定缓存的key,与@Cacheable注解相同。
- condition:指定缓存的条件,与@Cacheable注解相同。
- unless:指定缓存的条件,与@Cacheable注解相同。
- @CacheEvict
@CacheEvict注解可以标记方法,让方法清除缓存数据。该注解支持以下属性:
- value:指定缓存的名称,与@Cacheable注解相同。
- key:指定缓存的key,与@Cacheable注解相同。
- condition:指定缓存的条件,与@Cacheable注解相同。
- allEntries:是否清除缓存中的所有数据,默认值为false。
- beforeInvocation:是否在方法执行之前清除缓存,默认值为false。如果设为true,则在方法执行之前清除缓存数据。
##simple缓存
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
配置缓存
spring:
cache:
type: simple
开启缓存注解
@SpringBootApplication
@EnableCaching
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
注入缓存
以上三个注解可以结合使用,比如新增或更新数据时同时更新缓存,删除数据时同时清除缓存。例如:
@Cacheable(value = "myCache")
public String getValue(String key) {
// ...
}
@CachePut(value = "myCache", key = "#key")
public void updateValue(String key, String value) {
// ...
}
@CacheEvict(value = "myCache", key = "#key")
public void removeValue(String key) {
// ...
}
变更缓存ehcache
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
配置缓存
spring:
cache:
type: ehcache
ehcache:
config: ehcache.xml
ehcache.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://www.ehcache.org/ehcache.xsd"
updateCheck="false">
<diskStore path="D:\ehcache"/>
<cache name="myCache"
maxEntriesLocalHeap="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="true"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120">
</cache>
</ehcache>
元素解析:
<diskStore>元素
<diskStore>元素用于指定Ehcache缓存溢出到磁盘的位置。如果缓存中的元素超过了cache的限制,Ehcache会将元素写入该磁盘存储路径。
<cache>元素
在<cache>元素中,name属性指定了缓存名称为myCache,其余属性用于配置缓存中对象的行为和持久性。
- maxEntriesLocalHeap:缓存最大能够缓存的对象数量,如果缓存对象数量超过了该数量,较早使用且比较久未使用(LRU)的对象会被删除。
- eternal:表示缓存对象是否永久保存,如果设为true,对象永不过期;如果设为false,按照timeToIdleSeconds和timeToLiveSeconds来过期。
- timeToIdleSeconds:表示对象在缓存中闲置的时间(即没有被访问),超过时间则会被删除,而不是以过期的形式删除。
- timeToLiveSeconds:表示对象在缓存中可以存活的时间,超过时间则会被删除,而不是以过期的形式删除。
- overflowToDisk:表示当缓存对象超出缓存限制时是否将其保存到磁盘,设为true时表示会保存到磁盘上。
- diskPersistent:表示当缓存对象写入磁盘时,是否保证写入到磁盘上的数据可以持久保存,默认为false。
- diskExpiryThreadIntervalSeconds:表示缓存对象失效线程的运行时间间隔,单位为秒。如果缓存中的对象超过其生命周期,该线程会在指定时间间隔内检查所有存储在磁盘上的缓存中的元素是否已过期。
- memoryStoreEvictionPolicy:表示内存缓存淘汰机制,常用的有LRU和FIFO策略。
变更缓存redis
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
配置缓存
spring:
cache:
type: redis
redis:
key-prefix: user_ # 设置缓存的key前缀
use-key-prefix: true # 设置是否使用key前缀
cache-null-values: off # 设置是否缓存null值
enable-statistics: off # 设置是否开启统计功能
time-to-live: 10s # 设置缓存的过期时间
redis:
host: localhost
port: 6379