课程表

JFinal课程

工具箱
速查手册

JFinal EhCachePlugin

当前位置:免费教程 » Java相关 » JFinal

EhCachePlugin 是 JFinal 集成的缓存插件,通过使用 EhCachePlugin 可以提高系统的并发 访问速度。

EhCachePlugin 是作为 JFinal 的 Plugin 而存在的,所以使用时需要在 JFinalConfig 中配置EhCachePlugin,以下是 Plugin 配置示例代码:

public class DemoConfig extends JFinalConfig {
public void configPlugin(Plugins me) { me.add(new EhCachePlugin());
}
}


CacheInterceptor

CacheInterceptor 可以将 action 所需数据全部缓存起来,下次请求到来时如果 cache 存在则 直接使用数据并 render,而不会去调用 action。此用法可使 action 完全不受 cache 相关代码所 污染,即插即用,以下是示例代码:

@Before(CacheInterceptor.class)
public void list() {
List<Blog> blogList = Blog.dao.find("select * from blog"); User user = User.dao.findById(getParaToInt()); setAttr("blogList", blogList);
setAttr("user", user); render("blog.html");
}
上例中的用法将使用 actionKey 作为 cacheName,在使用之前需要在 ehcache.xml 中配置以 actionKey 命名的 cache 如:<cache name="/blog/list" …>,注意 actionKey 作为 cacheName 配置 时斜杠”/”不能省略。此外 CacheInterceptor 还可以与 CacheName 注解配合使用,以此来取代默认的 actionKey 作为actionName,以下是示例代码:

@Before(CacheInterceptor.class)
@CacheName("blogList")
public void list() {
List<Blog> blogList = Blog.dao.find("select * from blog"); setAttr("blogList", blogList);
render("blog.html");
}

以上用法需要在 ehcache.xml 中配置名为 blogList 的 cache 如:<cache name="blogList" …>。


EvictInterceptor

EvictInterceptor 可以根据 CacheName 注解自动清除缓存。以下是示例代码:

@Before(EvictInterceptor.class)
@CacheName("blogList")
public void update() { getModel(Blog.class).update(); redirect("blog.html");
}
上例中的用法将清除 cacheName 为 blogList 的缓存数据,与其配合的 CacheInterceptor 会 自动更新 cacheName 为 blogList 的缓存数据。


CacheKit

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()方法来 获取数据。


ehcache.xml 简介

EhCache 的使用需要有 ehcache.xml 配置文件支持,该配置文件中配置了很多 cache 节点, 每个 cache 节点会配置一个 name 属性,例如:,该属性是 CacheKit 取值所必须的。其它配置项如 eternal、 overflowToDisk、timeToIdleSeconds、 timeToLiveSeconds 详见 EhCache 官方文档。

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号