spring3.1的核心,对缓存做了实现。
主要是2个注解 @Cacheable 和@CacheEvict 并且很好的解决与业务代码的耦合度大的问题。
@Cacheable 这一注解可以在方法上或者类上加。在方法上加,是指该方法的返回结果存入缓存。在类上,就是所有的方法的返回结果都放入缓存。
缓存是以方法的参数为key,返回的结果为value的键值对。
下次访问,如果是同样的参数来执行该方法时,可以直接从缓存中获取结果。
当一个支持缓存的方法在对象内部被调用时是不会触发缓存功能的。
注解有3个属性:value,必须指定,表示返回值会存在哪个缓存上。如果是数组value={"cache1","cache2"}表示存到2个缓存
key,表示返回结果的key。 @Cacheable(value="users", key="#user.id")
如果key想要用方法名 #root.methodName 还有method、target、targetClass、args、caches。 某个root对象的属性、
condition,有时候我们并不想把所有的返回结果都放到缓存。这里就可以设置条件来限制。以springEL表达式来表示。如果返回true则缓存。
@Cacheable(value={"users"}, key="#user.id", condition="#user.id%2==0")
public User find(User user) {
return user;
}
@CachePut
不管你是否有缓存,每次都执行方法,并将结果放在缓存
@CacheEvict
标注在需要清除缓存元素的方法上。与cacheable类似。 value和key 可能与其他方法的@cacheable相同。这里清除。那么其他方法在执行时,会执行。因为在这里已经清除了。
allEntries,是否需要情况所有元素。如果是true就全部
CacheManager。用来管理Cache的接口。有基于jpa的ConcurrentMap,也有第三方的Ehcache。
这里我只说一下项目中,使用缓存和清除缓存。
首先也是对方法进行加@Cacheable。注意value(属于哪个缓存)和key(数据key的唯一性)
@Cacheable(value = CacheManager.CACHE_CUSTOMFIELD, key = "#moduleId")
public List<CustomField> getValids(String moduleId) {
QueryDefinition definition = new QueryDefinition();
definition.addCondition(CustomFields.EQUALS, moduleId);
return query(definition).getRecords();
}
@CacheEvict(value = Hdpos4CacheManager.CACHE_CUSTOMFIELD, key = "#field.getModuleId()")
public String effect(CustomField field, String customType){}
然后我们如果想,在程序中手动执行删除操作。
cacheManager.evictCache(Hdpos4CacheManager.CACHE_CUSTOMFIELD + moduleId);
我们会创建一个cacheManager来管理,这个类声明。
@Value("#{cacheManager.getCache('commonCache')}")
private Cache cache;
以及,pushCache方法和getCache方法。
cache.push(key,value)
cache.evict(key)
cache.clear()等一系列方法。是ehcache提供的一些方法。
private Cache getCacheByName(String cacheName) {
CacheManager cacheManager = ApplicationContextUtils.getBean(CacheManager.class);
return cacheManager.getCache(cacheName);
}
CacheManager是spring提供的管理所有的缓存。通过cacheManager.getCache()就可以获取。
可以在spring配置文件,使用ehCacheCacheManager插件。
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cache-manager-ref="ehcache" />
<bean id="ehcache"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:config-location="classpath:ehcache-spring.xml" />
<defaultCache maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="600" timeToLiveSeconds="600" overflowToDisk="true"
diskPersistent="false" diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU" />
<cache name="commonCache" maxElementsInMemory="20000" eternal="true"
timeToIdleSeconds="0" timeToLiveSeconds="0" overflowToDisk="false" />
<cache name="module" maxElementsInMemory="10000" eternal="true"
timeToIdleSeconds="0" timeToLiveSeconds="0" overflowToDisk="false" />
<cache name="org.hibernate.cache.StandardQueryCache"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="4200"
overflowToDisk="true"
/>
<cache name="org.hibernate.cache.UpdateTimestampsCache"
maxElementsInMemory="5000"
eternal="true"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
overflowToDisk="false"
/>
</ehcache>