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:

2 comments:

  1. Hi, i have add code in my view.jsp and create one controller as you suggest and add java code in that new controller, after deploying it says : Name : null cache is cleared...

    shall i have to make any changes for get running MultiVMPoolUtil ?

    ReplyDelete
  2. I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in Liferay, kindly Contact MaxMunus
    MaxMunus Offer World Class Virtual Instructor led training on Liferay. We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 1,00,000 + trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
    For Demo Contact us.
    Akash Kumar Asthana
    MaxMunus
    E-mail: Nitesh@maxmunus.com
    Skype id: nitesh_maxmunus
    Contact No: 8553912023
    www.MaxMunus.com


    ReplyDelete