Ehcache简单使用

一个ehcache的简单工具类,

测试环境

jdk1.6

Ehcahce ehcache-2.8.3

package com.utils;

import java.net.URL;

import com.Person;

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

public class CacheManagerUtil {
    private static CacheManagerUtil cacheManagerUtil;
    private static CacheManager cacheManager ;
    private static Cache cache;
    
    private CacheManagerUtil(){}
    /**
     * 初始化
     * @param cacheName
     * @return
     */
    public static synchronized CacheManagerUtil getInit(String cacheName){
        if(cacheManagerUtil==null){
            cacheManagerUtil = new CacheManagerUtil();
            URL url =  CacheManagerUtil.class.getResource("/ehcache.xml");
            cacheManager = CacheManager.create(url);
        }
        cache = cacheManager.getCache(cacheName);
        return cacheManagerUtil;
    }
    /**
     * 删除全部缓存
     */
    public static synchronized void removeAll(){
        cache.removeAll();
    }
    /**
     * 添加缓存
     * @param key
     * @param value
     */
    public synchronized void put(String key,Object value){
        Element element = new Element(key, value);
        cache.put(element);
    }
    /**
     * 获取缓存数据
     * @param key
     * @return
     */
    public synchronized Object get(String key){
        Element element = cache.get(key);
        Object value = element==null ? null:element.getObjectValue();
        return value;
    }
    public void close(){
        cacheManager.shutdown();
    }
    
    public static void main(String[] args){
        CacheManagerUtil util = CacheManagerUtil.getInit("DATA_DICTIONARY_CACHE");
        util.put("person1", new Person("王五", 20));
        util.put("person2", new Person("张三", 21));
        util.put("person3", new Person("李四", 22));
        
        System.out.println(util.get("person1"));
        System.out.println(util.get("person2"));
        System.out.println(util.get("person3"));
        
        util.close();
        
    }
    
}
ehcache.xml配置文件,放在根目录下

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
	monitoring="autodetect" dynamicConfig="true">

	<diskStore path="java.io.tmpdir" />

	<defaultCache maxEntriesLocalHeap="0" eternal="false"
		timeToIdleSeconds="1200" timeToLiveSeconds="1200">
	</defaultCache>

	<cache name="REMINDER_CACHE" maxElementsInMemory="10000"
		maxElementsOnDisk="10000000" overflowToDisk="true" eternal="false"
		timeToIdleSeconds="300" timeToLiveSeconds="600">
	</cache>
	
	<cache name="DATA_DICTIONARY_CACHE" maxElementsInMemory="10000"
		maxElementsOnDisk="10000000" overflowToDisk="true" eternal="true">
	</cache>

</ehcache>
person对象实例

package com;

import java.io.Serializable;

public class Person implements Serializable{
	private static final long serialVersionUID = 1L;
	private static long id = 1;
	private final long personId = id++;
	private String name;
	private int age;
	
	static{
		System.out.println("执行静态语句块");
	}
	
	public Person(){
		System.out.println("无参构造函数!");
	}
	
	public Person(String name ,int age){
		this.name = name;
		this.age = age;
		System.out.println("有参构造函数!");
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the age
	 */
	public int getAge() {
		return age;
	}

	/**
	 * @param age the age to set
	 */
	public void setAge(int age) {
		this.age = age;
	}

	/**
	 * @return the personId
	 */
	public long getPersonId() {
		return personId;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Person [personId=" + personId + ", name=" + name + ", age="
				+ age + "]";
	}
	
	
	
}

运行结果

执行静态语句块
有参构造函数!
有参构造函数!
有参构造函数!
Person [personId=1, name=王五, age=20]
Person [personId=2, name=张三, age=21]
Person [personId=3, name=李四, age=22]

你可能感兴趣的:(ehcache,java工具类)