Refresh Your Java - Before Java Interview
We are Agile, believe in less Documentation - Only Quick notes (Java Interview Questions) of Java/J2ee Read more....
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;
}
}
This Portal is intended to put all Java/J2ee related topics at one single place for quick referance, not only Technical , but also the Project Management Related thing such as Development Process methodoogies build process, unit testing etc.,
This Portal has More than 500 Java Interview Questions (also could be Considered as Quick Notes) very neatly separated topic by topic with simple diagrams which makes you easily understandable. Importantly these are from our Realtime expericance.