缓存过期策略
- FIFO(First In First out):先见先出,淘汰最先近来的页面,新进来的页面最迟被淘汰,完全符合队列。
- LRU(Least recently used):最近最少使用,淘汰最近不使用的页面
- LFU(Least frequently used): 最近使用次数最少, 淘汰使用次数最少的页面
- TTL(Time To Live):存活期,从缓存创建到到期的一个时间段
- TTI(Time to Idle):空闲期,一个数据多久没访问就过期
缓存例子
使用注解
@Cacheable(cacheName="users")
public User getUser(){
retun new User();
}
配置
<cache:annotation-driven/>
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"
p:name="default"/>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"
p:name="users"/>
</set>
</property>
</bean>
注解
@Cacheable:指定被注解的方法的返回值,是可以被缓存的,如果没有则缓存,如果有则返回
属性value/cachesNames:缓存名
key:缓存的key
condition:过滤条件 ture
unless:过滤条件 false
keyGenerator:键生成策略
//一下三个等价,表示使用users这个缓存
@Cacheable("users")
@Cacheable(value="users")
@Cacheable(userName="users")
//定义两个缓存
@Cacheable(userName={"users1","users"})
//方法已id作为键值缓存到users缓存段中
@Cacheable(cacheName="users")
public User getUser(String id){
retun new User(id);
}
//方法已user的id作为键值缓存到users缓存段中
@Cacheable(cacheName="users",key="user.id")
public User getUser(User user){
retun new User(user.getId());
}
//使用自定义的键生成策略,实现Generator接口
@Cacheable(cacheName="users",keyGenerator="myKeyGenerator")
//condition 过滤条件只要年龄大于10的缓存
@Cacheable(cacheName="users",condition="#user.age>10")
//与condition相反
@Cacheable(cacheName="users",unless="#user.age>10")
缓存的本质就是键值对的集合,默认使用方法签名以及参数值作为一个键,并将该键与方法调用的结果组成键值对,如果没有参数,则使用KeyGenerator生成Key
@CachePut:和 @Cacheable几乎一样,区别,他是用于更新或者添加,首先执行方法,然后把返回值放入缓存,也就是,只是有缓存,他也会执行方法。
@CacheEvict:和 @Cacheable相反,用于删除缓存
属性
value/cachesNames:缓存名
key:删除缓存的key
condition:删除缓存的过滤条件 ture
unless:删除缓存的过滤条件 false
allEntries:布尔,是否清除所有缓存元素 默认false
beforeInvocation:布尔,是否在方法执行前删除缓存,异常不删除默认false
//删除所有uses中的缓存
@CacheEvict(cacheNames="users",allEntries=true)
//方法执行之前删除缓存,不管是否是异常
//如果是false,表示只有方法正常返回才删除缓存,异常则不删除缓存
@CacheEvict(cacheNames="users",beforeInvocation=true)
- @Caching:组注解
//如果obj类型为Teacher 则放入Teacher缓存
//如果obj类型为User 则放入User缓存
@Caching(cacheable = {
@Cacheable(value = "Teacher", condition = "#obj instanceof T(com.sunjie.Teacher)"),
@Cacheable(value = "User", condition = "#obj instanceof T(com.sunjie.User)")
})
public Persion getPersion(Persion obj){
}
- @CacheConfig:类级别缓存注解
//整个类的方法都指定了缓存注解
@CacheConfig(cacheNames = "users",keyGenerator="MyKeyGenerator ")
public class UserService {
}
缓存管理器
- 列表内容SimpleCacheManager:可以配置缓存列表
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"
p:name="default"/>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"
p:name="users"/>
</set>
</property>
</bean>
- NoOpCacheManager:用于测试,不缓存数据
<bean id="cacheManager" class="org.springframework.cache.support.NoOpCacheManager">
- ConcurrentMapCacheManager:使用jdk 的ConcurrentMap
<bean id="cacheManager" class="org.springframework.cache.concurrent.ConcurrentMapCacheManager">
- CompositeCacheManager:定义多个缓存管理器
<bean id="cacheManager" class="org.springframework.cache.support.CompositeCacheManager">
<property name="cacheManagers">
......
</property>
</bean>
集成第三方缓存框架
- EhCache
第一步:导入jar
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>${version}</version>
</dependency>
第二步:定义配置
<cache:annotation-driven/>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cacheManager="ehcache"/>
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:configLocation="classpath:ehcache.xml"/>
第三步:定义EhCache.xml
<ehcache>
<cache name="users"
maxElementsInMemory="1000" />
</ehcache>
第四步:使用
//和之前的一样@Cacheable
....