Subversion Repositories SmartDukaan

Rev

Rev 3273 | Rev 11872 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.serving.cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;

public class EhcacheWrapper<K, V> 
{
    public static final String HOME_PAGE_SNIPPET_CACHE_NAME = "HomePageSnippets";
    public static final String SHOWCASE_PAGE_SNIPPET_CACHE_NAME = "ShowcasePageSnippets";
    public static final String PRODUCT_PAGE_SNIPPET_CACHE_NAME = "ProductPageSnippets";
    public static final String CATEGORY_SNIPPET_CACHE_NAME = "CategorySnippets";
    public static final String SEARCH_SNIPPET_CACHE_NAME = "SearchSnippets";
    
    public static enum CacheKeys{
        HOME_PAGE_SNIPPET_CACHE_KEY,
        SHOWCASE_PAGE_SNIPPET_CACHE_KEY,
        PRODUCT_PAGE_SNIPPET_CACHE_KEY,
        CATEGORY_SNIPPET_CACHE_KEY,
        SEARCH_SNIPPET_CACHE_KEY
    }
    
    private final String cacheName;
    private final CacheManager cacheManager;

    public EhcacheWrapper(final String cacheName, final CacheManager cacheManager)
    {
        this.cacheName = cacheName;
        this.cacheManager = cacheManager;
    }

    public void put(final K key, final V value)
    {
        getCache().put(new Element(key, value));
    }

    @SuppressWarnings("unchecked")
    public V get(final K key) 
    {
        Element element = getCache().get(key);
        if (element != null) {
            return (V) element.getObjectValue();
        }
        return null;
    }

    public void removeAll() 
    {
        getCache().removeAll();
    }
    
    public void remove(final K key) 
    {
        getCache().remove(key);
    }
    
    public Ehcache getCache() 
    {
        return cacheManager.getEhcache(cacheName);
    }
}