EHcache是 Java最普遍使用的一种Cache. 它能高效的减轻数据库的负载,同时有很好的扩展,支持集群, 是解决C10K问题的一把重要利器. 它使用简单,高速,实现线程安全,同时提供了用内存,磁盘文件存储,以及分布式存储方式等多种灵活的cache管理方案。同时 ehcache做为开放源代码项目,采用限制比较宽松的Apache License V2.0做为受权方式,被普遍地用于Hibernate, Spring,Cocoon等其余开源系统。 java
为何须要Cache? 数据库
Cache主要是用来提升系统的吞吐量. Cache主要是用来缓存CPU使用过其它DNS 系统的数据. 不少时候CPU以本地引用的方式不断去重复请求一样的数据. 这个时候Cache至关一块有"记忆"能力的空间,若是它记得你须要的数据,就直接传给请求者,若是不记得这个数据,才会像其它DNS系统发出请求. 缓存
因此有能够得出Cache几点很重要的做用: 安全
如何使用Cache? 异步
cache-hit: 请求的数据在缓存空间存在. 分布式
cache-miss:请求的数据在缓存空间中不存在. ui
命中率 : cache-hit/(cache-hit+cache-miss)Cache的关键在于它的命中率, Encache提供了三种缓存清空策略: this
FIFO:先进先出 开放源代码
LRU:最近最少使用 线程
LFU:最近不常用
FIFO:最近一次建立或者更新的,将被清空.
public class FifoPolicy extends AbstractPolicy { /** * The name of this policy as a string literal */ public static final String NAME = "FIFO"; /** * @return the name of the Policy. Inbuilt examples are LRU, LFU and FIFO. */ public String getName() { return NAME; } /** * Compares the desirableness for eviction of two elements * * Compares hit counts. If both zero, * * @param element1 the element to compare against * @param element2 the element to compare * @return true if the second element is preferable to the first element for ths policy */ public boolean compare(Element element1, Element element2) { return element2.getLatestOfCreationAndUpdateTime() < element1.getLatestOfCreationAndUpdateTime(); } }LRU:
最近最少使用, AccessTime最老的会被清掉.
public class LruPolicy extends AbstractPolicy { /** * The name of this policy as a string literal */ public static final String NAME = "LRU"; /** * @return the name of the Policy. Inbuilt examples are LRU, LFU and FIFO. */ public String getName() { return NAME; } /** * Compares the desirableness for eviction of two elements * * Compares hit counts. If both zero, * * @param element1 the element to compare against * @param element2 the element to compare * @return true if the second element is preferable to the first element for ths policy */ public boolean compare(Element element1, Element element2) { return element2.getLastAccessTime() < element1.getLastAccessTime(); } }LFU: 最少被使用,缓存的元素有一个hit属性,hit值最小的将会被清出缓存
public class LfuPolicy extends AbstractPolicy { /** * The name of this policy as a string literal */ public static final String NAME = "LFU"; /** * @return the name of the Policy. Inbuilt examples are LRU, LFU and FIFO. */ public String getName() { return NAME; } /** * Compares the desirableness for eviction of two elements * * Compares hit counts. If both zero, * * @param element1 the element to compare against * @param element2 the element to compare * @return true if the second element is preferable to the first element for ths policy */ public boolean compare(Element element1, Element element2) { return element2.getHitCount() < element1.getHitCount(); } }
附上此处UML类图
使用场景一:
Hibernate配置Ehcache来缓存持久化对像,大幅度提升了系统的IO和吞吐量。
使用场景二:
做者在实现一个异步调用的RestService时,处理完用户的请求后,常常须要反向调用用户提供的Rest接口,向用户报告处理结果。这个时候我使用了Ehcache+Spring annotation的方式来缓存处理同一个接口的RestClientProxy对像。
使用场景三:
使用Ehcache集群,适合多结点轻量级Server或者作DR时,备份结点能实时backup主结点的内存数据。