Loading
EhCache Query Example

Java Quick Notes

Refresh Your Java - Before Java Interview

We are Agile, believe in less Documentation - Only Quick notes (Java Interview Questions) of Java/J2ee Read more....


Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet

Go to SiteMap

Q)  EhCache Query Example


Ans)

ECache How to use Query Language , ehcache.CacheManager,searchAttribute,
query.addCriteria.

This Example demonstrates how to use Ecache query language to read
the Data from Cache.

Java Class that put and read the data from EHCache. In this Example
Ehcache query language is being used to read the data .

Step 1:

Value Object To Be stored in Cache :

public class CustomerData {
 
 private String firstName;
 private String lastName;
 private String dateOfBirth;
 private Integer age;
 
 
 public Integer getAge() {
  return age;
 }


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


 public String getFirstName() {
  return firstName;
 }


 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public String getDateOfBirth() {
  return dateOfBirth;
 }

 public void setDateOfBirth(String dateOfBirth) {
  this.dateOfBirth = dateOfBirth;
 }
}

Step 2:
EhCache Configuration :
 
   <cache name="CustomerCache"
      maxElementsInMemory="1000"
      eternal="true"
      overflowToDisk="false"
      diskPersistent="false"
      memoryStoreEvictionPolicy="LRU">
      <searchable>
    <searchAttribute name="customerId" expression="value.getCustomerId"/>
    <searchAttribute name="firstName" expression="value.getFirstName()"/>
    <searchAttribute name="age" expression="value.getAge()"/>
   </searchable>

  </cache> 


Step 3:

Java Class that put and read the data from EHCache. In this Example Ehcache query language
is being used to read the data .

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;
import net.sf.ehcache.search.Attribute;
import net.sf.ehcache.search.Direction;
import net.sf.ehcache.search.Query;
import net.sf.ehcache.search.Result;
import net.sf.ehcache.search.Results;

 

public Class EhcacheQueryExample {
 
 private static CacheManager cacheManager;
 
    private static CacheManager createEhCacheManager() {
 try {
     File f = new File(EHCACHE_PATH);
     BufferedInputStream bis = new BufferedInputStream( new FileInputStream(f));
     if (bis == null) {
     }
     cacheManager = CacheManager.create(bis);
 } catch (Exception e) {
 }
 return cacheManager;
    }
   
    public static CacheManager getCacheManager () {
     if (cacheManager == null) {
      cacheManager = createEhCacheManager();
     }
    }


 public static List<CustomerData> getCustomerDatay(Long firstName, Long age)  {
  List<CustomerData> customerListBean = new ArrayList();

  Ehcache cache=cacheManager.getCache("CustomerCache");
  Query query = cache.createQuery();
  
  Attribute<String> firstNameAttr = cache.getSearchAttribute("firstName");
  Attribute<Integer> ageAttr = cache.getSearchAttribute("age");
    
  query.includeKeys();
  query.includeValues();

  
  if(firstName !=null){
   query.addCriteria(firstNameAttr.eq(firstName));
  }
  
    if(age !=null){
   query.addCriteria(ageAttr.eq(age).or(ageAttr.eq(0L)));
  }
  
  query.addOrderBy(firstNameAttr, Direction.DESCENDING);
  query.addOrderBy(ageAttr, Direction.DESCENDING);
  
  Results results = query.execute();
  
  //Populate "discountListBean"
  for (Result result : results.all()) {
  }
  
  return customerListBean;
 }
 
}



Back to top

------------------------- We hope you got necessary Info On -----------------------------------------

EhCache Query Example


EhCache Query Example

-------------------------------------------------------------------------------------------------------



Face Book
Request for a Mock Interview/Training

Get a PDF

Face Book
Same look (Read) on any device, this is Ads free