深刻理解Ehcache系列(一)

      EHcache是 Java最普遍使用的一種Cache. 它能高效的減輕數據庫的負載,同時有很好的擴展,支持集羣, 是解決C10K問題的一把重要利器. 它使用簡單,高速,實現線程安全,同時提供了用內存,磁盤文件存儲,以及分佈式存儲方式等多種靈活的cache管理方案。同時 ehcache做爲開放源代碼項目,採用限制比較寬鬆的Apache License V2.0做爲受權方式,被普遍地用於Hibernate,  Spring,Cocoon等其餘開源系統。 java

      爲何須要Cache? 數據庫

       Cache主要是用來提升系統的吞吐量. Cache主要是用來緩存CPU使用過其它DNS 系統的數據. 不少時候CPU以本地引用的方式不斷去重複請求一樣的數據. 這個時候Cache至關一塊有"記憶"能力的空間,若是它記得你須要的數據,就直接傳給請求者,若是不記得這個數據,纔會像其它DNS系統發出請求. 緩存

      因此有能夠得出Cache幾點很重要的做用: 安全

  • 提升CPU的速度
  • 提升IO
  •  系統的更有彈性.

     如何使用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主結點的內存數據。

相關文章
相關標籤/搜索