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

一.關於EhCachePlugin

    在以前的文章中,咱們已經介紹過了JFinal中Cache的一些簡單使用,這篇文章將講述EhCachePlugin的使用, EhCachePlugin是JFinal集成的緩存插件,經過使用EhCachePlugin能夠提升系統的併發訪問速度。html

二.基本配置

  1.導入EhCachePlugin的相關jar包:、java

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是緩存操做工具類,可對緩存進行一系列的操做。併發

下面是實例:app

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,第三個參數則爲所要放入緩存的對象。ide

  除了get()以及put方法以外,CacheKit還有一下操做方法:工具

  • List getKeys(String cacheName):獲取名稱爲「cacheName」的cache中所有key。ui

  • 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:spa

如:

@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的緩存機制雖好用,但咱們要注意緩存操做並不適用與不少查詢。而對於一些在短時間內數據變更不大而且查詢複雜的數據而言,緩存可以很好的發揮其做用。

相關文章
相關標籤/搜索