spring boot 用缓存 存储用户相关的数据

spring boot 用缓存 存储用户相关的数据

    • 1、创建实体,存放缓存
    • 2、创建CacheManger 进行管理
    • 3、创建监听器,对缓存进行监听
    • 4、注入到容器中

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("启动缓存监听器...");
    }

}

你可能感兴趣的:(spring boot 用缓存 存储用户相关的数据)