# 7.3 CacheInterceptor
The CacheInterceptor
can cache all the data needed by an action. When a subsequent request comes in, if the cache exists, the system will directly use the data and render the page without calling the action. This approach ensures that the action is not polluted by any cache-related code and allows for plug-and-play functionality. Below is an example code snippet:
@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");
}
2
3
4
5
6
7
8
In the above example, the actionKey
is used as the cacheName
. Before using it, you need to configure a cache named after the actionKey
in ehcache.xml
, like <cache name="/blog/list" …>
. Note that when configuring the actionKey
as the cacheName
, the forward slash "/" should not be omitted.
Additionally, CacheInterceptor
can also be used in conjunction with the CacheName
annotation to replace the default actionKey
as the cacheName
. Here is an example code snippet:
@Before(CacheInterceptor.class)
@CacheName("blogList")
public void list() {
List<Blog> blogList = Blog.dao.find("select * from blog");
setAttr("blogList", blogList);
render("blog.html");
}
2
3
4
5
6
7
With the above code, you need to configure a cache named blogList
in ehcache.xml
like this:
<cache name="blogList" …>
By using the CacheInterceptor
and CacheName
annotation, you can make your caching strategy more flexible and keep your action methods clean from cache-specific logic.