CacheKit
CacheKit is a cache operation tool class. The following is a sample code:
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");
}
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");
}
The following code is an example of using the overloaded CacheKit.get(String, String, IDataLoader) method in CacheKit:
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");
}
List<Blog> blogList = CacheKit.get("blog", "blogList", newIDataLoader(){
public Object load() {
return Blog.dao.find("select * from blog");
}});
setAttr("blogList", blogList); render("blog.html");
}
The CacheKit.get method provides an IDataLoader interface. The load() method in this interface will be called only when the cache value does not exist. The specific operation process of this method is: first use cacheName=blog and key=blogList as parameters to cache and retrieve data. If the data in the cache exists, the data will be returned directly. If it does not exist, the IDataLoader.load() method will be called to obtain the data.