EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。
那么。如何搭建Ehcache的开发环境呢?
1、下载相关的jar包,这些jar包分别是:ehcache_2.10.1.jar、slf4j、log4j
2、编写配置文件ehcache.xml
好了,完成之后的项目结构图如下所示:
在Ehcache框架中,最重要的就是CacheManager这个类,那么,要如何得到这个类的一个实例呢?可以通过调用该类的静态方法create、getInstance和newInstance来得到该类的一个实例。以下,就是这三个方法的在CacheManager类中的实现源代码:
create方法:
public static CacheManager create(String configurationFileName) throws CacheException {
if (singleton != null) {
LOG.debug("Attempting to create an existing singleton. Existing singleton returned.");
return singleton;
}
synchronized (CacheManager.class) {
if (singleton == null) {
singleton = newInstance(configurationFileName);
} else {
LOG.debug("Attempting to create an existing singleton. Existing singleton returned.");
}
return singleton;
}
}
getInstance方法:
public static CacheManager getInstance() throws CacheException {
return CacheManager.create();
}
newInstance方法:
public static CacheManager newInstance() throws CacheException {
return newInstance(ConfigurationFactory.parseConfiguration(), "Creating new CacheManager with default config");
}
而singleton的定义为:
/**
* The Singleton Instance.
*/
private static volatile CacheManager singleton;
换句话说,getInstance首先会判断Singleton是否为空,不为空,返回当前的Singleton值,只有为空的时候,才调用newInstance创建一个Singleton返回。而newInstance则直接创建一个Singleton来返回。
好了,到目前为止,我们知道了如何得到一个CacheManager实例,现在就让我们用这个实例对象来进行缓存操作。
好了,让我们先来看看CacheUnits类的源代码:
public class CacheUnits {
private static CacheManager cacheManager = CacheManager.create("src/ehcache.xml");
private Cache getCache(String cacheName)
{
Cache cache = cacheManager.getCache(cacheName);
if(cache == null)
{
cacheManager.addCache(cacheName);
cache = cacheManager.getCache(cacheName);
cache.getCacheConfiguration().setEternal(true);
}
return cache;
}
//往缓存中写入数据
public void put(String cacheName , String key , Object obj)
{
Element element = new Element(key , obj);
getCache(cacheName).put(element);
}
//从缓存中取出数据
public Object get(String cacheName , String key)
{
Element element = getCache(cacheName).get(key);
return element == null ? null : element.getObjectValue();
}
//移除缓存中的数据
public void remove(String cacheName , String key)
{
getCache(cacheName).remove(key);
}
}
好了,再让我们写一个类来结合该CacheUnits和Mybatis数据库实现:当要读取的数据不存在于缓存中的时候,从数据库中找到相关的数据,并将其写入到缓存中,如果,缓存中存在这个数据的话,那么久不从数据库中取出该数据,而是直接从缓存中取出这条数据,从而避免频繁的读取数据库,是性能降低。
public class CacheServlet {
private static final String CACHE_PERSON_ID = "person_id_";
private static final String CACHE_PERSON_NAME = "person_name_";
private static final String CACHE_PERSON_ID_NUM = "person_id_num_";
PerDao dao = new PerDao();
CacheUnits cacheUnits = new CacheUnits();
public List<person> searchAll()
{
return dao.search(new person());
}
public person searchById(int id)
{
person result = (person) cacheUnits.get("personCache" , CACHE_PERSON_ID + id);
System.out.println(result);
if(result == null)
{
List<person> list = dao.search(new person(id));
if(list == null || list.size() == 0)
return null;
result = list.get(0);
cacheUnits.put("personCache" , CACHE_PERSON_ID+id , result);
}
return result;
}
public person searchByName(String name)
{
person result = (person)cacheUnits.get("personCache", CACHE_PERSON_NAME + name);
if(result == null)
{
List<person> list = dao.search(new person(name));
if(list == null || list.size() == 0)
{
return null;
}
result = list.get(0);
cacheUnits.put("personCache", CACHE_PERSON_NAME + name , result);
}
return result;
}
public person searchByIdNum(String id_num)
{
person result = (person)cacheUnits.get("personCache", CACHE_PERSON_ID_NUM + id_num);
if(result == null)
{
person per = new person();
per.setId_num(id_num);
List<person> list = dao.search(per);
if(list == null || list.size() == 0)
{
return null;
}
result = list.get(0);
cacheUnits.put("personCache", CACHE_PERSON_ID_NUM + id_num , result);
}
return result;
}
}
在该类中,分为通过id、name和id_num三种方式查询相关数据,当缓存中不存在下相关数据的时候,通过读写数据库来获取相关数据,然后再将其写入到缓存中,从而使下一次要获取相关的数据的时候,不再需要从数据库中读取,直接从缓存中就能够取出相关数据。
下面,让我们看看ehcache.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<ehcache updateCheck="false" name="defaultCache">
<diskStore path="java.io.tepdir" />
<!-- 默认缓存配置. -->
<defaultCache maxEntriesLocalHeap="100" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600"
overflowToDisk="true" maxEntriesLocalDisk="100000" />
<!-- 用户缓存 -->
<cache name="userCache" maxEntriesLocalHeap="100" eternal="true" overflowToDisk="true"/>
</ehcache>
好了,到目前为止,相信读者也能够使用Ehcache实现简单的缓存功能。
至于其他类的源代码以及配置文件的写法,请参照上一篇博文。