当前位置:K88软件开发文章中心网站服务器框架JFinal → 文章内容

6.5 CacheKit

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-25 14:22:09

由 厦门小懒懒 创建, 最后一次修改 2016-10-06 CacheKit 是缓存操作工具类,以下是示例代码:public void list() {List<Blog> blogList = CacheKit.get("blog", "blogList");if (blogList == null) {blogList = Blog.dao.find("select * from blog"); CacheKit.put("blog", "blogList", blogList);}setAttr("blogList", blogList); render("blog.html");}CacheKit 中最重要的两个方法是 get(String cacheName, Object key)与 put(String cacheName,Object key, Object value)。get 方法是从 cache 中取数据,put 方法是将数据放入 cache。参数 cacheName 与 ehcache.xml 中的<cache name="blog" …>name 属性值对应;参数 key 是指取值用 到的 key;参数 value 是被缓存的数据。以下代码是 CacheKit 中重载的 CacheKit.get(String, String, IDataLoader)方法使用示例:public void list() {List<Blog> blogList = CacheKit.get("blog", "blogList", newIDataLoader(){public Object load() {return Blog.dao.find("select * from blog");}});setAttr("blogList", blogList); render("blog.html");}CacheKit.get 方法提供了一个 IDataLoader 接口,该接口中的 load()方法在缓存值不存在时 才会被调用。该方法的具体操作流程是:首先以 cacheName=blog 以及 key=blogList 为参数去 缓存取数据,如果缓存中数据存在就直接返回该数据,不存在则调用 IDataLoader.load()方法来 获取数据。

6.5 CacheKit