1. 程式人生 > >JFinal框架學習-------EhCachePlugin

JFinal框架學習-------EhCachePlugin

一.關於EhCachePlugin

    在之前的文章中,我們已經介紹過了JFinal中Cache的一些簡單使用,這篇文章將講述EhCachePlugin的使用, EhCachePlugin是JFinal整合的快取外掛,通過使用EhCachePlugin可以提高系統的併發訪問速度。

二.基本配置

  1.匯入EhCachePlugin的相關jar包:、

2.ehcache.xml配置檔案

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
         updateCheck="false" monitoring="autodetect"
         dynamicConfig="true">

    <!--指定一個檔案,當ehcache把資料寫到硬碟上時,會預設把資料寫到該檔案下-->
    <!--user.home - 使用者主目錄;user.dir - 使用者當前工作目錄;java.io.tmpdir - 預設臨時檔案路徑。-->
    <diskStore path="java.io.tmpdir" />

    <!-- 設定快取的預設資料過期策略 -->
    <defaultCache maxElementsInMemory="10000"
                  eternal="true"
                  timeToIdleSeconds="3600"
                  timeToLiveSeconds="3600"
                  overflowToDisk="true">
    </defaultCache>

    <!--自定義cache-->
    <cache  name="user/list"
            maxElementsInMemory="10000"
            maxElementsOnDisk="1000"
            eternal="false"
            overflowToDisk="true"
            timeToIdleSeconds="900"
            timeToLiveSeconds="1800"
            memoryStoreEvictionPolicy="LFU">
    </cache>

</ehcache>

3.在JFinal的基本配置類中新增EhCachePlugin外掛:

 @Override
    public void configPlugin(Plugins me) {
        DruidPlugin druidPlugin=creatDruidPlugins();
        me.add(druidPlugin);
        ActiveRecordPlugin arp=new ActiveRecordPlugin(druidPlugin);

        //建立了資料庫表到Model的對映
        _MappingKit.mapping(arp);
        me.add(arp);

        //配置快取外掛
        me.add(new EhCachePlugin());
    }

 

三.CacheKit

    CacheKit是快取操作工具類,可對快取進行一系列的操作。

下面是例項:

 public void text(){
        User user = new User().dao();
        List<User> users = CacheKit.get("cacheTest","userlist");
        if(users == null){
            users = user.find("select * from user");
            CacheKit.put("cacheTest","userlist",users);
        }
        setAttr("userList",users);
       render("index.html");
    }

   CacheKit可以對快取進行讀寫操作,CacheKit.get()方法中,第一個引數為cache的名稱,這個應與ehcache.xml中所配置的name相同,第二個引數為key表示著取值時的物件。在CacheKit.put()向快取中放資料的方法中,第一個引數為cache的名稱,第二個引數為取值時用到的key,第三個引數則為所要放入快取的物件。

  除了get()以及put方法之外,CacheKit還有一下操作方法:

  • List getKeys(String cacheName):獲取名稱為“cacheName”的cache中全部key。

  • remove(String cacheName, Object key):刪除某一快取。
  • removeAll(String cacheName):刪除cache中的全部快取。
  • static Cache getOrAddCache(String cacheName):根據cacheName獲取或建立cache。

 

四.CacheInterceptor

   除了使用CacheKit對快取進行操作之外,JFinal還提供了 CacheInterceptor攔截器,該攔截器可以將action所需資料全部快取起來,下次請求到來時如果cache存在則直接使用資料並render,而不會去呼叫action。使用該用法之前需要將ehcache.xml中的cache的name命名為action:

如:

@Before(CacheInterceptor.class)
public void list() {
    User user1 = getModel(User.class);
    UserService userService = new UserService();
    userService.add(user1);
    setAttr("userList", userService.queryUsetrList());
    render("index.html");
}

  此時,cache的name將為<cache name="/user/list"  ...>

若cache的name為自定義的,則可使用@CacheName(" cachename ") 註解來取代actionkey。

 

五.EvictInterceptor

    EvictInterceptor可以根據CacheName註解自動清除快取。

如:

@Before(EvictInterceptor.class)
    public void update(){
        getModel(User.class).update();
        render("index.html");
    }

  EvictInterceptor與CacheInterceptor的配合可以很好地更新快取的資料。

  以下是EvictInterceptor的原始碼:

  我們可以看到,攔截器被呼叫後,將呼叫CacheKit.removeAll()方法將快取全部清除。

 

   六.

     JFinal的快取機制雖好用,但我們要注意快取操作並不適用與很多查詢。而對於一些在短期內資料變動不大並且查詢複雜的資料而言,快取能夠很好的發揮其作用。