美文网首页
03EhCacheUtils 工具类

03EhCacheUtils 工具类

作者: 安以北往南 | 来源:发表于2019-07-30 14:08 被阅读0次
    import net.sf.ehcache.Cache;
    import net.sf.ehcache.CacheManager;
    import net.sf.ehcache.Element;
    
    /**
     * @ClassName:EhCacheUtil
     * @Description: <p> Ehcache工具类   </p>
     * @Author: - Jason
     * @CreatTime:2019/7/26 - 9:21
     * @Modify By:
     * @ModifyTime: 2019/7/26
     * @Modify marker:
     * @version V1.0
    */
    public class EhCacheUtil {
    
        /**
         * @Description: 获取缓存
         * @param cacheName 缓存名字
         * @return Cache
         * @Autor: Jason
         */
        private static Cache getCache(String cacheName) {
            CacheManager cacheManager = CacheManager.getInstance();
            if (null == cacheManager) {
                return null;
            }
            Cache cache = cacheManager.getCache(cacheName);
            if (null == cache) {
                return null;
            }
            return cache;
        }
    
        /**
         * @Description:    新增缓存记录
         * @param cacheName 缓存名字
         * @param key       缓存key
         * @Autor: Jason
         */
        public static void put(String cacheName, String key, Object value) {
            Cache cache = getCache(cacheName);
            if (null != cache) {
                Element element = new Element(key, value);
                cache.put(element);
            }
        }
    
        /**
         * @Description:    删除缓存记录
         * @param cacheName 缓存名字
         * @param key       缓存key
         * @return boolean  是否成功删除
         * @Autor: Jason
         */
        public static boolean remove(String cacheName, String key) {
            Cache cache = getCache(cacheName);
            if (null == cache) {
                return false;
            }
            return cache.remove(key);
        }
    
        /**
         * @Description: 删除全部缓存记录
         * @param cacheName 缓存名字
         * @Autor: Jason
         */
        public static void removeAll(String cacheName) {
            Cache cache = getCache(cacheName);
            if (null != cache) {
                //logOnRemoveAllIfPinnedCache();
                cache.removeAll();
            }
        }
    
        /**
         * @Description: 获取缓存记录
         * @param cacheName  缓存名字
         * @param key        缓存key
         * @return Object    缓存记录数据Object
         * @Autor: Jason
         */
        public static Object get(String cacheName, String key) {
            Cache cache = getCache(cacheName);
            if (null == cache) {
                return null;
            }
            Element cacheElement = cache.get(key);
            if (null == cacheElement) {
                return null;
            }
            return cacheElement.getObjectValue();
        }
    
    
    }
    
    

    相关文章

      网友评论

          本文标题:03EhCacheUtils 工具类

          本文链接:https://www.haomeiwen.com/subject/akfnrctx.html