spring bootは、ユーザに関するデータをキャッシュで記憶する.

24351 ワード

spring bootは、ユーザに関するデータをキャッシュで記憶する.
  • 、エンティティを作成し、キャッシュ
  • を格納する.
  • 、CacheMangerを作成して管理する
  • 、モニターを作成し、キャッシュを傍受する
  • .
  • 、容器に注入する
  • .
    1、エンティティを作成し、キャッシュを保存する
    
    public class EntityCache {
        /**
         *      
         */
        private  Object datas;
     
        /**
         *         , 0      
         */
        private  long timeOut;
     
        /**
         *       
         */
        private  long lastRefeshTime;
     
        public EntityCache(Object datas, long timeOut, long lastRefeshTime) {
            this.datas = datas;
            this.timeOut = timeOut;
            this.lastRefeshTime = lastRefeshTime;
        }
        public Object getDatas() {
            return datas;
        }
        public void setDatas(Object datas) {
            this.datas = datas;
        }
        public long getTimeOut() {
            return timeOut;
        }
        public void setTimeOut(long timeOut) {
            this.timeOut = timeOut;
        }
        public long getLastRefeshTime() {
            return lastRefeshTime;
        }
        public void setLastRefeshTime(long lastRefeshTime) {
            this.lastRefeshTime = lastRefeshTime;
        }
    }
    
    
    2、CacheMangerを作成して管理する
    
    import java.util.Map;
    import java.util.Set;
    import java.util.concurrent.ConcurrentHashMap;
    
    public class CacheManager {
    	private static Map<String, EntityCache> caches = new ConcurrentHashMap<String, EntityCache>();
    	 
        /**
         *     
         * @param key
         * @param cache
         */
        private static void putCache(String key, EntityCache cache) {
            caches.put(key, cache);
        }
     
        /**
         *     
         * @param key
         * @param cache
         */
        public static void putCache(String key, Object datas, long timeOut) {
            timeOut = timeOut > 0 ? timeOut : 0L;
            putCache(key, new EntityCache(datas, timeOut, System.currentTimeMillis()));
        }
     
        /**
         *       
         * @param key
         * @return
         */
        public static EntityCache getCacheByKey(String key) {
            if (isContains(key)) {
                return caches.get(key);
            }
            return null;
        }
     
        /**
         *       
         * @param key
         * @return
         */
        public static Object getCacheDataByKey(String key) {
            if (isContains(key)) {
                return caches.get(key).getDatas();
            }
            return null;
        }
     
        /**
         *       
         * @param key
         * @return
         */
        public static Map<String, EntityCache> getCacheAll() {
            return caches;
        }
     
        /**
         *         
         * @param key
         * @return
         */
        public static boolean isContains(String key) {
            return caches.containsKey(key);
        }
     
        /**
         *       
         */
        public static void clearAll() {
            caches.clear();
        }
     
        /**
         *       
         * @param key
         */
        public static void clearByKey(String key) {
            if (isContains(key)) {
                System.out.println(key + "      ");
                caches.remove(key);
            }
        }
     
        /**
         *         
         * @param key
         * @return
         */
        public static boolean isTimeOut(String key) {
            if (!caches.containsKey(key)) {
                return true;
            }
            EntityCache cache = caches.get(key);
            long timeOut = cache.getTimeOut();
            if(timeOut == 0) {
                // 0,    
                return false;
            }
            long lastRefreshTime = cache.getLastRefeshTime();
            if (timeOut != 0 && System.currentTimeMillis() - lastRefreshTime >= timeOut) {
                return true;
            }
            return false;
        }
     
        /**
         *     key
         * @return
         */
        public static Set<String>  getAllKeys() {
            return caches.keySet();
        }
    }
    
    3、モニターを作成し、キャッシュを傍受する
    public class CacheListener {
    
        public void startListen() {
            new Thread() {
                public void run() {
                    while (true) {
                        for (String key : CacheManager.getAllKeys()) {
                            if (CacheManager.isTimeOut(key)) {
                                CacheManager.clearByKey(key);
                            }
                        }
                    }
                }
            }.start();
        }
    }
    
    4、容器に注入する
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.stereotype.Component;
    
    import zone.pro.controller.common.cache.CacheListener;
    
    
    @Component
    public class InitLoading implements CommandLineRunner {
    
    	@Override
    	public void run(String... arg0) throws Exception {
    		// TODO Auto-generated method stub
    		//startCacheListener();
    	}
    	
    	public void startCacheListener() {
            
            CacheListener cacheListener = new CacheListener();
            cacheListener.startListen();
            System.out.println("       ...");
        }
    
    }