Databases Reference
In-Depth Information
Liferay caching API
In Chapter 4 , Caching Best Practices , we discussed Liferay's caching capabilities in
detail. We talked about different cache pools used by Liferay Portal. We talked
about the cluster-enabled multi-VM cache pool and nonclustered single-VM cache
pool. Liferay provides an API to add custom cache buckets in both cache pools. This
API can be used by custom portlets or plugins for caching. By using Liferay's caching
APIs, we can leverage Liferay's built-in cache support. We do not need to worry
about cache replication, cache monitoring, or cache configuration. They are handled
at the portal level.
To use Liferay's caching API, first we need to decide which cache pool we want to
use. As discussed in Chapter 4 , Caching Best Practices , a single-VM cache pool is ideal
for resources that are unique per node and do not require cache replication. Here is
an example class from a custom portlet that utilizes a single-VM pool:
package com.connectsam.development;
import com.liferay.portal.kernel.cache.SingleVMPoolUtil;
import java.util.ArrayList;
import java.util.List;
public class SingleVMPoolExample {
public List<String> getTestList(String key){
List<String> listOfStrings = null;
//Retrieve List from Single-VM Pool by passing Cache Name and Key of
cached object
listOfStrings = (List<String>) SingleVMPoolUtil.get("com.connectsam.
development.SingleVMPoolExample",key);
if(listOfStrings == null){
//If object not found in cache then retrieve the object from source
listOfStrings = getSampleList();
//Put the retrieved object in cache
SingleVMPoolUtil.put( "com.connectsam.development.SingleVMPoolExample"
,key, listOfStrings);
}
return listOfStrings;
}
private List<String> getSampleList(){
List<String> list = new ArrayList<String>();
list.add("Single VM List");
return list;
}
}
 
Search WWH ::




Custom Search