Spring ehCache 示例

http://blog.chinaunix.net/uid-20577907-id-2834484.html


Ehcache的类层次模型主要为三层,最上层的是CacheManager,他是操作Ehcache的入口。我们可以通过CacheManager.getInstance()获得一个单子的CacheManger,或者通过CacheManger的构造函数创建一个新的CacheManger。每个CacheManager都管理着多个Cache。而每个Cache都以一种类Hash的方式,关联着多个Element。而Element则是我们用于存放要缓存内容的地方。
在配置EhCache前需要引入两个开发包:ehcache-1.3.0.jar和commons-logging-1.04.jar
配置文件
例子:ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>

<ehcache>


	<defaultCache maxElementsInMemory="2" eternal="false"
		timeToIdleSeconds="1" timeToLiveSeconds="1" overflowToDisk="false"
		memoryStoreEvictionPolicy="LRU" />



	<cache name="sampleCache1" maxElementsInMemory="5" eternal="false"
		overflowToDisk="false" timeToIdleSeconds="1" timeToLiveSeconds="1"
		memoryStoreEvictionPolicy="LRU">
 
	</cache>

	<cache name="userCache" maxElementsInMemory="5" eternal="false"
		overflowToDisk="false" timeToIdleSeconds="1" timeToLiveSeconds="1"
		memoryStoreEvictionPolicy="LRU">

	</cache>

</ehcache>


注:在ehcache的配置文件里面必须配置defaultCache。每个<cache>标签定义一个新的cache,属性的含义基本上可以从名字上得到,详细的说明可以参考上面的链接。
示例程序:
例子:
import java.util.List;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

public class TestEhCache
{

    public static void main(String[] args) throws Exception
    {
        CacheManager manager = new CacheManager("ehcache.xml");
        Cache cache = manager.getCache("sampleCache1");
        for (int i = 0; i < 6; i++)
        {
            Element e = new Element("key" + i, "value" + i);
            cache.put(e);
        }

        List<String> keys = cache.getKeys();
        for (String key : keys)
        {
            System.out.println(key + "," + cache.get(key));
        }
        cache.remove("key2");

        Cache userCache = manager.getCache("userCache");
        userCache.put(new Element("1", new User("1")));
        userCache.put(new Element("2", new User("2")));

        keys = userCache.getKeys();
        for (String key : keys)
        {
            System.out.println(key + "," + userCache.get(key));
        }

    }
}


public class User
{
    private String id;
    private String name;
 
}



注:程序的流程也是比较明晰的,首先是获取一个CacheManager,这是使用Ehcache的入口,然后通过名字获取某个Cache,然后就可以对Cache存取Element。Cache使用类Hash的方式来管理Element。
事件处理
说明:可以为CacheManager添加事件监听,当对CacheManager增删Cache时,事件处理器将会得到通知。要配置事件处理,需要通过ehcache的配置文件来完成。
配置文件:ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>

<ehcache>


	<defaultCache maxElementsInMemory="2" eternal="false"
		timeToIdleSeconds="1" timeToLiveSeconds="1" overflowToDisk="false"
		memoryStoreEvictionPolicy="LRU" />



	<cache name="sampleCache1" maxElementsInMemory="5" eternal="false"
		overflowToDisk="false" timeToIdleSeconds="1" timeToLiveSeconds="1"
		memoryStoreEvictionPolicy="LRU">
		<cacheEventListenerFactory class="EhCacheListen" />

	</cache>

 

</ehcache>


注:通过<cacheManagerEventListenerFactory>来注册事件处理器的工厂类。
代码:
import java.util.Properties;
import net.sf.ehcache.CacheException;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;
import net.sf.ehcache.event.CacheEventListener;
import net.sf.ehcache.event.CacheEventListenerFactory;

public class EhCacheListen extends CacheEventListenerFactory
{
    @Override
    public CacheEventListener createCacheEventListener(Properties properties)
    {
        return new CEL();
    }
}

class CEL implements CacheEventListener
{
    public void dispose()
    {
    }

    public void notifyElementEvicted(Ehcache cache, Element element)
    {
    }

    public void notifyElementExpired(Ehcache cache, Element element)
    {
    }

    public void notifyElementPut(Ehcache cache, Element element) throws CacheException
    {
        System.out.println(element.getKey() + " was added.");
    }

    public void notifyElementRemoved(Ehcache cache, Element element) throws CacheException
    {
        System.out.println(element.getKey() + " was removed.");
    }

    public void notifyElementUpdated(Ehcache cache, Element element) throws CacheException
    {
    }

    public void notifyRemoveAll(Ehcache cache)
    {
    }

    @Override
    public Object clone() throws CloneNotSupportedException
    {
        return super.clone();
    }
}


注:这里的代码与之前的类似,由此可见Ehcache的事件处理采用的是一种类plugin方式,也就是说,事件处理的添加是以不修改源代码为前提的。

猜你喜欢

转载自bdk82924.iteye.com/blog/1856663