Ehcache

添加依赖


    net.sf.ehcache
    ehcache
    2.8.3

需要添加仓库才能找到ehcache依赖


    ...
    
        jboss-maven2-release-repository
        https://repository.jboss.org/nexus/content/groups/developer/ 
    

在src/mian/resource添加ehcache.xml



    
    
    
    
        
    


先看一个单独使用的例子,之后和spring结合

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

public class HelloEhCache{
    public static void main(String[] args) {
        //1. Create a cache manager
        //CacheManager cm = CacheManager.create();使用默认配置文件创建 
        CacheManager cm = CacheManager.create("/ehcache.xml");
        
        //cm.addCache("movieFindCache2");设置一个名为movieFindCache2的新cache,属性为默认 
        
        //2. Get a cache called "movieFindCache", declared in ehcache.xml
        Cache cache = cm.getCache("movieFindCache");
        
        //3. Put few elements in cache
        cache.put(new Element("1","Jan"));
        cache.put(new Element("2","Feb"));
        cache.put(new Element("3","Mar"));
        
        //4. Get element from cache
        Element ele = cache.get("2");
        
        //5. Print out the element
        String output = (ele == null ? null : ele.getObjectValue().toString());
        System.out.println(output);
        
        //6. Is key in cache?
        System.out.println(cache.isKeyInCache("3"));
        System.out.println(cache.isKeyInCache("10"));
        
        //7. shut down the cache manager
        cm.shutdown();   
    }
}

要点:

CacheManager cm = CacheManager.create("/ehcache.xml");
Cache cache = cm.getCache("movieFindCache");
cache.put(new Element("1","Jan"));

参考:
http://sishuok.com/forum/posts/list/315.html
http://www.mkyong.com/ehcache/ehcache-hello-world-example/)
ehcache另有商用版BigMemory,参考:http://carlosfu.iteye.com/blog/2239561

你可能感兴趣的:(Ehcache)