zoukankan      html  css  js  c++  java
  • 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());
  • 相关阅读:
    js数组与字符串的相互转换方法
    js页面跳转常用的几种方式
    js刷新页面方法大全
    IIS上开启反向代理实现Vue项目接口跨域处理
    【问题解决记录】vue解决低版本安卓与ios10以下系统兼容性问题
    【解决问题记录】https网站中请求http资源接口报错与netERRSSLPROTOCOLERROR错误的解决
    indexedDb数据库基本操作
    Object常用方法
    htmlToTex
    禁止鼠标右键保存/拖动/选中/复制 图片/文字
  • 原文地址:https://www.cnblogs.com/vicF/p/12111492.html
Copyright © 2011-2022 走看看