ehcache通过配置,可以实现诸如,内容最长缓存时间,内容最长没有被访问时间等功能,当以上时间expired,缓存中的内容将清空。
ehcache 缓存策略:
read-only:无需修改, 那么就可以对其进行只读 缓存,注意,在此策略下,如果直接修改数据库,即使能够看到前台显示效果,
但是将对象修改至cache中会报error,cache不会发生作用。另:删除记录会报错,因为不能在read-only模式的对象从cache中删除。
read-write:需要更新数据,那么使用读/写缓存 比较合适,前提:数据库不可以为serializable transaction isolation level
(序列化事务隔离级别)
nonstrict-read-write:只偶尔需要更新数据(也就是说,两个事务同时更新同一记录的情况很不常见),也不需要十分严格的事务隔离,
那么比较适合使用非严格读/写缓存策略。
ehcache如何根据数据库的修改更新缓存内容?
可以为ehcache定制修改监听器,当监听的内容被修改时,clear相应内容。
<cache
name="commandCache"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="0"
overflowToDisk="true">
<cacheEventListenerFactory class="com.iding.pos.server.cache.CommandCacheEventListenerFactory"/>
</cache>
public class OrderCacheEventListener implements CacheEventListener {
protected static final Logger logger = LoggerFactory.getLogger(OrderCacheEventListener.class);
public void notifyElementRemoved(Ehcache ehcache, Element element) throws CacheException {
//To change body of implemented methods use File | Settings | File Templates.
}
public void notifyElementPut(Ehcache ehcache, Element element) throws CacheException {
//To change body of implemented methods use File | Settings | File Templates.
}
public void notifyElementUpdated(Ehcache ehcache, Element element) throws CacheException {
//To change body of implemented methods use File | Settings | File Templates.
}
public void notifyElementExpired(Ehcache ehcache, Element element) {
PosService posService = (PosService) ServiceLocator.getService("posService");
// 订单没有正常被POS机处理,所以在这里需要设置订单状态
long orderId = (Long)element.getObjectValue();
logger.debug("the order of:" + orderId + " have expired.");
posService.updateOrderStatus(orderId, Const.POS_RECIVE_FAILD);
// 从缓存中清除
ehcache.remove(element.getObjectKey());
// 如果长时间没反应则可能POS机有问题(这里做一下标识)
}
public void notifyElementEvicted(Ehcache ehcache, Element element) {
//To change body of implemented methods use File | Settings | File Templates.
}
public void notifyRemoveAll(Ehcache ehcache) {
//To change body of implemented methods use File | Settings | File Templates.
}
public void dispose() {
//To change body of implemented methods use File | Settings | File Templates.
}
public Object clone() throws CloneNotSupportedException {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
}
分享到:
相关推荐
cache、ehcache等缓存使用,实现存对象读对象等等
mybatis添加ehcache缓存支持,基于maven项目管理
EHCache缓存的说明文档是到处找来的内容,都有参考链接指向原地址。有三个测试项目也在里面:一个整合了Struts2或Hibernate,一个整合了MyBatis3,这两个是我做的;另一个ehcachetest是下载了別人的。
Ehcache 是一种广泛使用的开源 Java 分布式缓存。主要面向通用缓存,Java EE 和轻量级容器。它具有内存和磁盘存储,缓存加载器,缓存扩展,缓存异常处理程序,一个 gzip 缓存 servlet 过滤器,支持 REST 和 SOAP api...
EHcache缓存框架,ehcache介绍与说明,Ehcache详细,EHcache集群环境配置
Ehcache缓存技术 Ehcache缓存技术 Ehcache缓存技术 Ehcache缓存技术 Ehcache缓存技术
两级缓存在redis的方案上做一步优化,在缓存到远程redis的同时,缓存一份到本地进程ehcache(此处的ehcache不用做集群,避免组播带来的开销),取缓存的时候会先取本地,没有会向redis请求,这样会减少应用服务器<–...
对数据缓存技术的一个总结,文档比较全面的阐述了缓存类型,功能特性和相关技术。
ehcache页面缓存技术ehcache页面缓存技术ehcache页面缓存技术ehcache页面缓存技术ehcache页面缓存技术ehcache页面缓存技术ehcache页面缓存技术ehcache页面缓存技术ehcache页面缓存技术ehcache页面缓存技术ehcache...
ehcache缓存页面 ehcache缓存页面
Ehcache缓存配置说明帮助你如何使用Ehcache
Ehcache缓存配置
ehcache缓存的jar包和配置文件。
hibernate最核心的 就是缓存技术
简单的ehcache缓存demo实例,描述了 ehcache缓存的简单使用
ehcache缓存依赖的jar
springboot2.0使用ehcache缓存,这个压缩包时一个简单的demo。
本工程用于研究如何借助Ehcache缓存框架实现对页面的缓存 本工程编码方式:UTF-8 本工程开发工具:MyEclipse 说明: 1、ehcache.xml和ehcache.xsd两个文件可以在下在下载下来的名为“ehcache-core-x.x.x-...
ehcache缓存
ehcache缓存配置详解 个人通过查资料和实践后总结