• java实现一个本地缓存工具类


    本地缓存实现大概思路,单例模式创建本地缓存实例 + 定时器定时扫描缓存是否过期

    代码如下

    package webapp.cache;
    
    import java.util.concurrent.ConcurrentHashMap;
    import java.util.concurrent.TimeUnit;
    
    /**
     * @Author:vic
     * @Date:Created in 16:55 2019/12/27
     * @Description:本地缓存
     */
    public class LocalCache {
    
        /**缓存默认失效时间(毫秒)*/
        private static final long DEFAULT_TIMEOUT = 3600*1000;
    
        /**缓存清除动作执行间隔(秒)*/
        private static final long TASK_TIME = 1;
    
        /**缓存存储的map*/
        private static final ConcurrentHashMap<String,CacheEntity> cacheMap = new ConcurrentHashMap<>();
    
    
        public LocalCache() {
        }
    
        private static LocalCache cache = null;
    
        public static LocalCache getInstance() {
         //单例一下
    if (cache == null) { cache = new LocalCache(); new Thread(new TimeoutTimer()).start(); } return cache; }
    //定时器线程-用于检查缓存过期
    static class TimeoutTimer implements Runnable{ @Override public void run(){ while (true) { try { TimeUnit.SECONDS.sleep(TASK_TIME); for (String key:cacheMap.keySet()) { CacheEntity entity = cacheMap.get(key); long now = System.currentTimeMillis(); if ((now - entity.getTimeStamp()) >= entity.getExpire()) { cacheMap.remove(key); } } } catch (InterruptedException e) { e.printStackTrace(); } } } } /**存储单元*/ static class CacheEntity { /***/ private Object value; /**过期时间(毫秒)*/ private long expire; /**创建时的时间戳*/ private long timeStamp; public Object getValue() { return value; } public void setValue(Object value) { this.value = value; } public long getExpire() { return expire; } public void setExpire(long expire) { this.expire = expire; } public long getTimeStamp() { return timeStamp; } public void setTimeStamp(long timeStamp) { this.timeStamp = timeStamp; } } public static boolean set(String key,Object value,long expire) { cacheMap.put(key,setEntity(key,value,expire)); return true; } public static boolean set(String key,Object value) { cacheMap.put(key,setEntity(key,value,DEFAULT_TIMEOUT)); return true; } private static CacheEntity setEntity(String key,Object value,long expire){ CacheEntity entity = new CacheEntity(); entity.setValue(value); entity.setExpire(expire); entity.setTimeStamp(System.currentTimeMillis()); return entity; } public static Object get(String key) { CacheEntity entity = cacheMap.get(key); if (entity == null) { return null; } else { Object value = entity.getValue(); if (value == null) { return null; } return value; } } public static void remove(String key) { cacheMap.remove(key); } }

    调用示例

    String key = "key";
    String value = "value";
            
    LocalCache cache = LocalCache.getInstance();
    cache.set(key,value,5000);
    Object result = cache.get(key);
    System.out.println(result.toString());
  • 相关阅读:
    poj 2262
    poj 1050
    poj 1730
    poj 1061
    【设计模式】简单工厂模式学习
    【待学】
    [设计模式]策略模式和单一职责
    Windows live writer 误删 草稿 恢复
    [Linq]Linq To Sql (2)
    MVC 学习日志1(上)
  • 原文地址:https://www.cnblogs.com/vicF/p/12111492.html
Copyright © 2020-2023  润新知