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");
}
The two most important methods in CacheKit are get(String cacheName, Object key) and put(String cacheName, Object key, Object value). The get method is to retrieve data from the cache, and the put method is to put the data into the cache. The parameter cacheName corresponds to the <cache name="blog" ...>name attribute value in ehcache.xml; the parameter key refers to the key used to obtain the value; the parameter value is the cached data.



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");
}


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.