Saturday 31 March 2012

EHCache Basics

Ehcache is easy and very powerful object caching system.
First step is to download the distribution. From the archive you need the
  • ehcache-core-2.3.1.jar - jar file contains everything that is need to use ehcache.
  • ehcache.xsd - schema for the ehcache configuration file.

First you need to create configuration file ehcache.xml.
ehcache.xml
  
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="ehcache.xsd"
         updateCheck="true" monitoring="autodetect"
         dynamicConfig="true">

    <!-- Specify cache storage location -->
    <diskStore path="/tmp/opencourzesupport/cache"/>
    <defaultCache
      maxElementsInMemory="0"
      eternal="false"
      timeToIdleSeconds="0"
      timeToLiveSeconds="0"
      overflowToDisk="false" />
    <cache name="MyCache"
           maxElementsInMemory="0"
           maxElementsOnDisk="5000"
           eternal="false"
           overflowToDisk="true"
           diskSpoolBufferSizeMB="20"
           timeToIdleSeconds="300"
           timeToLiveSeconds="600"
           memoryStoreEvictionPolicy="LFU"
           transactionalMode="off"
            />
</ehcache>

Configuration defines storage directory and cache with name "MyCache".
You need to specify default cache too, else it will throw exception.
Let's define the object that we are going to put into cache:
 
package com.opensourzesupport.ehcache.model;
/**
 *
 * @author prasobh
 */
public class Student {
    private String name;
    private int age;
    private String address;
    private long id;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }
    

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }   
    
    @Override
    public String toString() {
        return "Student{" + "name=" + name + ", age=" + age + ", address=" + address + ", id=" + id + '}';
    }
}

This is java code that implements caching logic:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.opensourzesupport.ehcache;

import com.opensourzesupport.ehcache.model.Student;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

/**
 *
 * @author prasobh
 */
public class TestCache {

    public TestCache() {
        CacheManager cm = new CacheManager(this.getClass().getResourceAsStream("/com/opensourzesupport/ehcache/resource/ehcache.xml"));
        Cache cache = cm.getCache("MyCache");
        Student student = new Student();
        student.setId(1);
        student.setName("Prasobh.K");
        student.setAge(26);
        student.setAddress("OpenSourzeSupport");
         cache.put(new Element(1, student));
        Student cachedBean = (Student) cache.get(1).getObjectValue();

        System.out.println("Model from cacahe : " + cachedBean);
    }

    public static void main(String[] args) {
        new TestCache();

    }
}

Output :
Model from cacahe :  Student{name=Prasobh.K, age=26, address=OpenSourzeSupport, id=1}

No comments:

Post a Comment