Showing posts with label MultiVMPoolToolUtil. Show all posts
Showing posts with label MultiVMPoolToolUtil. Show all posts

Sunday, January 31, 2016

Liferay Cache Using MultiVMPoolUtil

Liferay Version: Liferay 6.2

In this post we will see how to use liferay cache.
In this example we will put values in cache inside render method and will get that cache value in jsp.
We can put cache in other places also.

STEPS:
Step1: Create one liferay mvc project name: Caching of portlet plugin type.  Now create one portlet  inside that project .In this example portlet class/controller name: Cache.java
Step 2: Add below code in portlet view.jsp, path: docroot/html/caching/view.jsp
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@page import="com.liferay.portal.kernel.util.Validator"%>
<%@page import="com.liferay.portal.model.User"%>
<%@page import="java.util.List"%>
<%@page import="com.liferay.portal.kernel.cache.MultiVMPoolUtil"%>
<portlet:defineObjects />

<%
String name = (String)MultiVMPoolUtil.getCache("my-Cache").get("Name");
List<User> users =(List<User>)MultiVMPoolUtil.getCache("myList-Cache").get("listValue");
%>

Name : <%=name%>
<%
if(Validator.isNull(users)){
       out.println("cache is cleared...");
} else{
       for(User userInfo: users){
              %>
              <p style="color:green;"><%=userInfo.getEmailAddress()%></p><br/>
        <%
        }
}
%>


Step 3: Add below code in portlet controller, in this example controller name: Cache.java
package com.test;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;

import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.model.User;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;

/**
 * Portlet implementation class Cache
 */
public class Cache extends MVCPortlet {

        @Override
              public void render(RenderRequest request, RenderResponse response)
                           throws PortletException, IOException {
                    
                  String name = "Azharuddin";
                     List<User>  userList = null;
                      try {
                            userList = UserLocalServiceUtil.getUsers(-1, UserLocalServiceUtil.getUsersCount());
                     } catch (SystemException e) {
                           // TODO Auto-generated catch block
                           e.printStackTrace();
                     }
                      MultiVMPoolUtil.getCache("myList-Cache").put("listValue", (Serializable)userList);
                      MultiVMPoolUtil.getCache("my-Cache").put("Name",name);
                      // TODO Auto-generated method stub
                     super.render(request, response);
              }
      


}


After deploying portlet.


OUTPUT: