ehCacheService

public class CacheService
{
    private static CacheManager cacheManager = null;
    static{
        cacheManager = CacheManager.create(Thread.currentThread().getContextClassLoader().getResourceAsStream("config/ehcache.xml"));
    }
    private static CacheManager getCacheManager(){
        return cacheManager;                           
    }

    public static Serializable getCachedObject(String cacheName, String elementName){
        Serializable result = null;
        try{
            Cache cache = getCacheManager().getCache(cacheName);
            Element element = cache.get(elementName);
            if(null != element && !cache.isExpired(element)){
                result = element.getValue();
            }
            else if(null != element && cache.isExpired(element)){
                Serializable o = element.getValue();
                if(o instanceof ArrayList){
                    ArrayList list = (ArrayList) o;
                    for(int i=list.size()-1;i>=0;i--){
                        list.remove(i);
                    }                   
                }
                o = null;
                cache.remove(elementName);           
            }
        } catch (Exception e){
            System.out.println("CacheService:getCachedObject:" + e.getMessage());
}
       
        return result;
    }
    
    public static void putObjectInCache(String cacheName, String elementName, Serializable object){
        try{
            Cache cache = getCacheManager().getCache(cacheName);
            Element element = new Element(elementName, object);
            cache.put(element);
        }
        catch(Exception e){
            System.out.println("CacheService:putObjectInCache:" + e.getMessage());
        }       
    }
    
    public static void removeCachedObject(String cacheName,String elementName){
Serializable result = null;
try{
Cache cache = getCacheManager().getCache(cacheName);
Element element = cache.get(elementName);
if(null != element){
Serializable o = element.getValue();
if(o instanceof ArrayList){
ArrayList list = (ArrayList) o;
for(int i=list.size()-1;i>=0;i--){
list.remove(i);
}                   
}
o = null;
cache.remove(elementName);           
}
}
catch(IllegalStateException e){
System.out.println("CacheService:removeCachedObject:" + e.getMessage());
}
catch (CacheException e){
System.out.println("CacheService:removeCachedObject:" + e.getMessage());
}
    }
   
    public static String[] getCacheNames() {
    return getCacheManager().getCacheNames();
    }
    
    public static Cache getCache(String cacheName) {
    return getCacheManager().getCache(cacheName);
    }   
   
   
    public static void empty(){
        getCacheManager().shutdown();
    }
   
    public static void clear(String cacheName) {
Cache cache = getCacheManager().getCache(cacheName);
if(cache != null) {
cache.removeAll();
}   
    }
    
    public static void clearAll() {
getCacheManager().clearAll();
    }
}

你可能感兴趣的:(thread,xml,cache)