PrimeFaces 5 Cache Example Integrating Ehcache

By Arvind Rai, February 01, 2015
This page will provide PrimeFaces 5 cache example integrating Ehcache. PrimeFaces 5 cache component helps to reduce page load time by caching data. By default PrimeFaces 5 provides default cache management but for production environment we need to integrate other cache providers like ehcache and hazelcast. Here in this page we are using ehcache with PrimeFaces 5. The cache tag is used as below.
<p:cache id="" region="" key="" rendered =""> 
It has different attributes described a below.
region: Unique id for cache region.
key: Unique id of cache entry in region.
id: Unique id of component.
rendered: This is a boolean value and if false, component will not be rendered.

Project Structure in Eclipse

For quick understanding, find the print screen of project structure in eclipse.
PrimeFaces 5  Cache Example  Integrating  Ehcache

build.gradle

To use Ehcache in the application we need to resolve the Ehcache JAR dependency. Find the gradle file.
build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'war'
archivesBaseName = 'primefaces5'
version = '1' 
repositories {
    mavenCentral()
}
dependencies {
    compile 'com.sun.faces:jsf-api:2.2.9'
    compile 'com.sun.faces:jsf-impl:2.2.9'
    compile 'org.primefaces:primefaces:5.1'
    compile 'javax.enterprise:cdi-api:1.2'
    compile 'net.sf.ehcache:ehcache:2.9.0'
} 

ehcache.xml

To support Ehcache, we need to add ehcache.xml in our application classpath. Find the ehcache.xml.
ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
	<defaultCache eternal="true" maxElementsInMemory="100" overflowToDisk="false" />
	<cache name="emp" maxElementsInMemory="10000" eternal="true" overflowToDisk="false" />
</ehcache> 

Add primefaces.CACHE_PROVIDER in web.xml

To support Ehcache, we need to add context parameter in web.xml i.e primefaces.CACHE_PROVIDER with the value org.primefaces.cache.EHCacheProvider
web.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="3.0">
	<display-name> PrimeFaces 5  Cache Example  Using  Ehcache </display-name>
    <context-param>
	<param-name>primefaces.CACHE_PROVIDER</param-name>
	<param-value>org.primefaces.cache.EHCacheProvider</param-value>
    </context-param>
    <servlet>
      <servlet-name>FacesServlet</servlet-name>
      <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
      <servlet-name>FacesServlet</servlet-name>
      <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
      <servlet-name>FacesServlet</servlet-name>
      <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
</web-app> 

Create View using cache Tag

We are creating XHTML with cache tag. For the demo, we are displaying some data of list using PrimeFaces dataTable tag. Because of cache tag, list will be fetched only first time from managed bean and onwards data displayed within cache tag will be cached and no hit to server will be observed.
students.xhtml
<html lang="en"
      xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"
	  xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title> PrimeFaces 5  Cache Example  Using  Ehcache </title>
    </h:head>
    <h:body>
       <h3>PrimeFaces 5  Cache Example  Using  Ehcache </h3>
		<h:form>
		  <p:cache region="dtcache" key="dttable">
		     <p:dataTable value="#{dataTableView.studentList}" var="student">
			    <p:column headerText="Id">
			        <h:outputText value="#{student.id}" />
			    </p:column>
			    <p:column headerText="Name">
			        <h:outputText value="#{student.name}" />
			    </p:column>
		    </p:dataTable>
		 </p:cache>   
	        </h:form>
    </h:body>
</html>	 

Managed Bean for View

We are creating a managed bean for the cache demo.
DataTableView.java
package com.concretepage;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
@ManagedBean(name = "dataTableView")
@ViewScoped
public class DataTableView implements Serializable {
	private static final long serialVersionUID = 1L;
	private  List<Student> studentList = new ArrayList<Student>();
        @PostConstruct
        public void init() {
    	   //add students
    	   studentList.add(new Student(111, "Ram"));
    	   studentList.add(new Student(222, "Mahesh"));
    	   studentList.add(new Student(333, "Krishna"));
        }
	public List<Student> getStudentList() {
	   System.out.println("Fetch student data.");
	   return studentList;
	}
	static public class Student implements Serializable {
        private Integer id;
        private String name;
        public Student(Integer id,String name) {
            this.id = id;
            this.name = name;
        }
	public Integer getId() {
	    return id;
	}
	public String getName() {
	    return name;
	}
    }
} 

Test Application for Cache

To test the cache application, deploy the war file in tomcat and access the URL http://localhost:8080/primefaces5-1/students.xhtml
For the first time we will get the hit in our server log as below
Fetch student data. 
For the next refresh, we will not get server hit because data is cached.

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us