Home  >  JSF 2  >  PrimeFaces 5

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


To use Ehcache in the application we need to resolve the Ehcache JAR dependency. Find the gradle file.
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'war'
archivesBaseName = 'primefaces5'
version = '1' 
repositories {
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'


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

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
<?xml version="1.0" encoding="ISO-8859-1" ?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
	<display-name> PrimeFaces 5  Cache Example  Using  Ehcache </display-name>

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.
<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">
        <title> PrimeFaces 5  Cache Example  Using  Ehcache </title>
       <h3>PrimeFaces 5  Cache Example  Using  Ehcache </h3>
		  <p:cache region="dtcache" key="dttable">
		     <p:dataTable value="#{dataTableView.studentList}" var="student">
			    <p:column headerText="Id">
			        <h:outputText value="#{student.id}" />
			    <p:column headerText="Name">
			        <h:outputText value="#{student.name}" />

Managed Bean for View

We are creating a managed bean for the cache demo.
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")
public class DataTableView implements Serializable {
	private static final long serialVersionUID = 1L;
	private  List<Student> studentList = new ArrayList<Student>();
        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


©2022 concretepage.com | Privacy Policy | Contact Us