6.3 CacheInterceptor

2022-07-11 10:24 更新

CacheInterceptor 可以將 action 所需數(shù)據(jù)全部緩存起來,下次請(qǐng)求到來時(shí)如果 cache 存在則 直接使用數(shù)據(jù)并 render,而不會(huì)去調(diào)用 action。此用法可使 action 完全不受 cache 相關(guān)代碼所 污染,即插即用,以下是示例代碼:

@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 配置 時(shí)斜杠”/”不能省略。此外 CacheInterceptor 還可以與 CacheName 注解配合使用,以此來取代默認(rèn)的 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" …>。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)