
三月,你不能跳吗? >>> 
@Cacheable(值=“ accountCache”),此注释表示调用此方法时,它将从名为accountCache的缓存中查询,如果没有,则执行实际方法(即查询),并存储执行结果进入缓存,否则返回缓存中的对象. 缓存中的键是参数userName,值是Account对象. “ accountCache”缓存是在Spring * .xml中定义的名称.
value: 缓存位置名称spring ehcache 缓存,不能为空,如果使用EHCache,则为ehcache.xml中声明的缓存名称
key: 缓存的密钥,默认情况下为空. 它指示该方法的参数类型和参数值用作键. 支持SpEL.
condition: 触发条件. 仅将满足条件的条件添加到缓存中. 默认值为空,这表示所有内容都已添加到缓存中. 支持SpEL.
@Cacheable(value="accountCache",key="#userId")// 使用了一个缓存名叫 accountCache
public Account getAccountByName(String userId) {
// 方法内部实现不考虑缓存逻辑,直接实现业务
return getFromDB(userId);
} @Cacheable(value="accountCache",condition="#userName.length() <=4")// 缓存名叫 accountCache
public Account getAccountByName(String userName) {
// 方法内部实现不考虑缓存逻辑,直接实现业务
return getFromDB(userName);
}
@CacheEvict批注以标记清除缓存的方法. 调用此方法时,将清除缓存. 请注意,@ CacheEvict(值=“ accountCache”,键=“#account.getName()”)之一,其中键用于指定缓存键,因为在保存时我们使用帐户对象的名称字段因此,在这里您还需要从参数帐户对象获取键的名称值. 前面的#号表示这是SpEL表达式. 该表达式可以遍历方法的参数对象. 有关特定的语法,请参阅Spring的相关文档.
value: 缓存位置名称,不能为空,如上
key: 缓存键,默认为空,同上.

condition: 触发条件. 仅在满足条件时才清除缓存. 默认为空. 支持SpEL. 有关SpEL文档,请参阅:
allEntries: true表示清除所有缓存中的值,默认为false
@CacheEvict(value="accountCache",key="#account.getName()")// 清空accountCache 缓存
public void updateAccount(Account account) {
updateDB(account);
}
@CacheEvict(value="accountCache",allEntries=true)// 清空accountCache 缓存
public void reload() {
reloadAll()
}
@CachePut批注,此批注可以确保该方法被执行,并且该方法的返回值也记录在缓存中,以实现缓存和的同步更新.
值
spring配置文件中定义的缓存名称必须至少指定一个
示例:
@Cacheable(值=“ mycache”)或
@Cacheable(值= {“ cache1”,“ cache2”}

缓存键可以为空. 如果指定根据SpEL表达式编写,则如果未指定,则默认情况下将根据方法的所有参数进行组合.
示例:
@Cacheable(值=“ testcache”,键=“#userName”)
条件
缓存条件可以为空,可用SpEL编写,返回true或false,仅当为true时缓存
示例:
@Cacheable(值=“ testcache”spring ehcache 缓存,条件=“#userName.len
通常适用于更新数据,因此还可以更新数据并且还可以更新缓存中的数据.
@CachePut(value="accountCache",key="#account.getName()")// 更新accountCache 缓存
public Account updateAccount(Account account) {
return updateDB(account);
} CacheManager cacheManager = CacheManager.create();
// 或者
cacheManager = CacheManager.getInstance();
// 或者
cacheManager = CacheManager.create("/config/ehcache.xml");
// 或者
cacheManager = CacheManager.create("http://localhost:8080/test/ehcache.xml");
cacheManager = CacheManager.newInstance("/config/ehcache.xml");
// .......
// 获取ehcache配置文件中的一个cache
Cache sample = cacheManager.getCache("sample");
// 获取页面缓存
BlockingCache cache = new BlockingCache(cacheManager.getEhcache("SimplePageCachingFilter"));
// 添加数据到缓存中
Element element = new Element("key", "val");
sample.put(element);
// 获取缓存中的对象,注意添加到cache中对象要序列化 实现Serializable接口
Element result = sample.get("key");
// 删除缓存
sample.remove("key");
sample.removeAll();
// 获取缓存管理器中的缓存配置名称
for (String cacheName : cacheManager.getCacheNames()) {
System.out.println(cacheName);
}
// 获取所有的缓存对象
for (Object key : cache.getKeys()) {
System.out.println(key);
}
// 得到缓存中的对象数
cache.getSize();
// 得到缓存对象占用内存的大小
cache.getMemoryStoreSize();
// 得到缓存读取的命中次数
cache.getStatistics().getCacheHits();
// 得到缓存读取的错失次数
cache.getStatistics().getCacheMisses();

spring-ehcache.xml配置
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">
<!-- cacheManager工厂类,指定ehcache.xml的位置 -->
<bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation">
<value>classpath:ehcache.xml</value>
</property>
</bean>
<!-- 声明cacheManager -->
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager">
<ref local="cacheManagerFactory" />
</property>
</bean>
<!-- 启用缓存注解功能,这个是必须的,否则注解不会生效-->
<cache:annotation-driven cache-manager="cacheManager"/>
</beans> <?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="true" monitoring="autodetect">
<diskStore path="java.io.tmpdir"/>
<!-- <diskStore path="E:/cachetmpdir" /> -->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="600"
timeToLiveSeconds="1200"
overflowToDisk="false"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="1800"
memoryStoreEvictionPolicy="LRU"
/>
<cache name="productCache"
maxElementsInMemory="10000"
eternal="false"
overflowToDisk="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU">
</cache>
</ehcache>
maxElementsInMemory: 可以在缓存中存储的最大元素数. 如果放置在缓存中的元素数超过此值,则有两种情况: 1.如果overflowToDisk的属性值为true,则缓存中的多余元素将放置在磁盘文件中. 2.如果overflowToDisk属性值为false,则将根据memoryStoreEvictionPolicy的策略替换缓存中的原始元素.
eternal: 表示是否永远留在内存中. 如果值为true,则缓存中的元素将始终存储在内存中,并且不会因超时而丢失,因此,如果此值为true,则timeToIdleSeconds和timeToLiveSeconds属性的值将不起作用.
timeToIdleSeconds: 是此缓存中访问元素之间的最大间隔. 如果在此之后未访问缓存中的元素,则将从缓存中清除该元素.
timeToLiveSeconds: 这是缓存中元素的生存期. 它表示从在缓存中创建元素到其过期的时间,以及从创建开始算起的时间. 超过此时间后,将从缓存中清除此元素.
overflowToDisk: 是否将溢出写入磁盘. 系统将根据标记
diskExpiryThreadIntervalSeconds: 磁盘缓存的清理线程的运行间隔
memoryStoreEvictionPolicy: 内存存储和释放策略. 有三个值:

LRU -最近最少使用
LFU -最不常用
FIFO-先进先出,创建时间最久的元素
diskPersistent: 是否保留磁盘缓存. 当此属性的值为true时,系统将在初始化期间在磁盘中查找包含文件名缓存名称和后缀索引的文件,例如CACHE_FUNC.index. 此文件存储已保留在磁盘中的缓存的索引. 找到后,将缓存加载到内存中. 要真正将缓存持久化到磁盘,在编写程序时必须小心. 在使用net.sf.ehcache.Cache的void put(元素元素)方法之后,请使用void flush()方法.
以上时间值以秒为单位.
参考:
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-146377-1.html
但如果战争爆发我会第一时间到战地医院去
非金钱非利益能定而