• 如何配置缓存移除缓存


    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;
        }
        
    }

  • 相关阅读:
    Java High Level REST Client 中文API(仅供参考)
    3.2_springBoot2.1.x检索之JestClient操作ElasticSearch
    3.1_springboot2.x检索之elasticsearch安装&快速入门
    2.2_springboot2.x消息RabbitMQ整合&amqpAdmin管理组件的使用
    2.1_springboot2.x消息介绍&RabbitMQ运行机制
    1.2_springboot2.x中redis缓存&原理介绍
    1.1_springboot2.x与缓存原理介绍&使用缓存
    JavaWeb-----Get请求和Post请求
    Java基础(basis)-----TCP通信
    Java基础(basis)-----泛型详解
  • 原文地址:https://www.cnblogs.com/huangwentian/p/7366326.html
Copyright © 2020-2023  润新知