zoukankan      html  css  js  c++  java
  • 如何配置缓存移除缓存

    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Collections;
    import java.util.LinkedHashSet;
    import java.util.List;
    import java.util.Map;

    import org.apache.commons.lang3.StringUtils;

    import com.avtrace.nlc.cctv.entity.TreeBase;

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

    /**
     * 缓存处理抽象基类
     *
     * @author AVT-WCB
     *
     */
    public abstract class CacheBase {
        
        /**
         * 得到缓存键
         *
         * @return
         */
        protected abstract String getCacheKey();

        private static final String DEVICE_CACHE = "device_dynamic_cache";
        
        /**
         * 得到缓存
         * @return
         */
        public Object getCache(){
            Ehcache cache = getCache(false);
            if(cache == null) {
                return null;
            }
            Element element = cache.get(getCacheKey());
            Object result = element == null ? null : element.getObjectValue();
            return result;
        }
        
        /**
         * 设置缓存
         * @param val
         */
        public void setCache(Object val){
            Ehcache cache = getCache(true);
            Element element = new Element(getCacheKey(),val,null,1200,1200);
            cache.put(element);
        }
        
        /**
         * 移除数据缓存
         *
         * @return
         */
        public void removeCache() {
            removeCache(getCacheKey());
        }

        /**
         * 移除数据缓存
         *
         * @param key
         *            :缓存键
         */
        public void removeCache(String key) {
            Ehcache cache = getCache(false);
            if (cache != null && !StringUtils.isEmpty(key)) {
                cache.remove(key);
            }
        }
        
        /**
         * 得到所有缓存键
         *
         * @return
         */
        @SuppressWarnings("unchecked")
        public List<String> getCacheKeys(){
            Ehcache cache = getCache(false);
            if(cache != null){
                return cache.getKeys();
            }
            return null;
        }
        
        /**
         * 得到缓存对象
         * @param isCreateIfNotExists:当不存在时,是否创建
         * @return 缓存对象
         */
        private Ehcache getCache(boolean isCreateIfNotExists){
            CacheManager manager = net.sf.ehcache.CacheManager.getInstance();
            Ehcache cache = manager.getEhcache(DEVICE_CACHE);
            if(isCreateIfNotExists && cache == null){
                synchronized(this){
                    cache = manager.getEhcache(DEVICE_CACHE);
                    if(cache == null){
                        manager.removeCache(DEVICE_CACHE);
                        try{
                            manager.addCache(DEVICE_CACHE);
                        } catch(Exception ex){
                            //第一次使用时总是报这个错,但不影响正常使用,抽空再详细研究
                            //throw new ObjectExistsException("Cache " + cacheName + " already exists");
                        }
                        cache = manager.getCache(DEVICE_CACHE);
                    }
                }
            }
            return cache;
        }
        
        /**
         * 得到所有父节点,包括自己(有序唯一集合)
         *
         * @param hashMap
         *            :所有树形结构Map
         * @param id
         *            :查找ID
         * @return 所有父节点列表包括自己
         */
        public Collection<String> getParents(final Map<String, ? extends TreeBase> hashMap, final String id){
            if(hashMap != null && hashMap.containsKey(id)){
                Collection<String> list = new LinkedHashSet<>();
                list.add(id);
                
                String pId = id;
                TreeBase obj = hashMap.get(pId);
                while(obj != null && !StringUtils.isEmpty(obj.getParentId())){
                    pId = obj.getParentId();
                    if(!list.add(pId)){
                        break;
                    }
                    obj = hashMap.get(pId);
                }
                
                return list;
            }
            return Collections.emptyList();
        }
        
        /**
         * 得到所有父节点,包括自己(有序唯一集合)
         *
         * @param hashMap
         *            :所有树形结构Map
         * @param id
         *            :查找ID
         * @return 所有父节点列表包括自己
         */
        public List<String> getParentsList(final Map<String, ? extends TreeBase> hashMap, final String id){
            if(hashMap != null && hashMap.containsKey(id)){
                List<String> list = new ArrayList<>();
                list.add(id);
                
                String pId = id;
                TreeBase obj = hashMap.get(pId);
                while(obj != null && !StringUtils.isEmpty(obj.getParentId())){
                    pId = obj.getParentId();
                    if(!list.add(pId)){
                        break;
                    }
                    obj = hashMap.get(pId);
                }
                
                return list;
            }
            return null;
        }
        
    }

  • 相关阅读:
    chrome浏览器实现上传下载功能
    Python安装第三方库时报错的解决办法
    python文本编辑器Visual Studio Code
    python下载安装
    selenium环境配置
    linux_安装浏览器
    在linux中安装selenium+chrome
    armbian_配置selenium
    armbian_运行puppeteer
    java环境配置
  • 原文地址:https://www.cnblogs.com/huangwentian/p/7366326.html
Copyright © 2011-2022 走看看