Adding the results in the Cache

If we want our results to be in cache, Then we can follow the below procedure as

Suppose we take an example, where users to be add in the cache. where name is "ListOfUsers" , key is "Users" and value is getting the all users - UserLocalServiceUtil.getAllUsers(-1,-1).

First line is calling the cache called "ListOfUsers".
Second line is getting the cache by key "Users".
Then the preceding line is , if cache value is null the put the values in the cache.

PortalCache cache = MultiVMPoolUtil.getCache("ListOfUsers");
List<user> userList = (List<user>) cache.get("Users");
if(Validator.isNull(userList)) {
cache.put("Users",UserLocalServiceUtil.getAllUsers(-1,-1),100);
}

Here 100 refers to the no.of seconds the results to be in the cache. After 100 seconds cache will be cleared.

Comments

Popular posts from this blog

How to know which liferay version we are using

Viewing the SQL Query in liferay : debugging the SQL Query in Hibernate

Opening portlet in popup