一個簡單的 ehcache 操作封裝

by57 9年前發布 | 2K 次閱讀 Java

CacheWrapper.java

public interface CacheWrapper<K, V>
{
  void put(K key, V value);

V get(K key); }</pre>

EhcacheWrapper.java

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

public class EhcacheWrapper<K, V> implements CacheWrapper<K, V> { 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));
}

public V get(final K key) 
{
    Element element = getCache().get(key);
    if (element != null) {
        return (V) element.getValue();
    }
    return null;
}

public Ehcache getCache() 
{
    return cacheManager.getEhcache(cacheName);
}

}</pre>

 本文由用戶 by57 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!