Java 使用 Map 实现缓存工具

以下代码参考于网上,做了小部分修改。

该代码实现了定时清除临时缓存的功能。

缓存管理类

  1 package com.wbproject.util.cache;
  2 
  3 import java.time.LocalDateTime;
  4 import java.time.format.DateTimeFormatter;
  5 import java.util.Date;
  6 import java.util.HashMap;
  7 import java.util.HashSet;
  8 import java.util.Iterator;
  9 import java.util.Map;
 10 import java.util.Set;
 11 
 12 /**
 13  * 缓存管理类
 14  *
 15  * @author wangbo
 16  * @date 2018-03-07 12:43:41
 17  */
 18 public class CacheManage {
 19 
 20     private static Map<Object, Object> cacheMap = new HashMap<>();
 21 
 22     private static Map<Object, CacheConfModel> cacheConfMap = new HashMap<>();
 23 
 24     private static CacheManage cm = null;
 25 
 26     // 构造方法私有化
 27     private CacheManage() {
 28     }
 29 
 30     // 获取实例
 31     public static CacheManage getInstance() {
 32         if (cm == null) {
 33             cm = new CacheManage();
 34             // 第一次获取实例的时候启动线程
 35             Thread t = new ClearCache();
 36             t.start();
 37         }
 38         return cm;
 39     }
 40 
 41     /**
 42      * 添加缓存实体
 43      * 
 44      * @param key
 45      * @param value
 46      * @param ccm
 47      * @return
 48      */
 49     public boolean addCache(Object key, Object value, CacheConfModel ccm) {
 50         System.out.println("开始增加缓存");
 51         boolean flag = false;
 52         try {
 53             cacheMap.put(key, value);
 54             cacheConfMap.put(key, ccm);
 55             System.out.println("增加缓存结束");
 56             flag = true;
 57         } catch (Exception e) {
 58             e.printStackTrace();
 59         }
 60 
 61         return flag;
 62     }
 63 
 64     /**
 65      * 获取缓存实体
 66      * 
 67      * @param key
 68      * @return
 69      */
 70     public Object getValue(Object key) {
 71         Object object = cacheMap.get(key);
 72         if (object != null) {
 73             return object;
 74         } else {
 75             return null;
 76         }
 77     }
 78 
 79     /**
 80      * 获取缓存数据的数量
 81      * 
 82      * @return
 83      */
 84     public int getSize() {
 85         return cacheMap.size();
 86     }
 87 
 88     /**
 89      * 删除缓存
 90      * 
 91      * @param key
 92      * @return
 93      */
 94     public boolean removeCache(Object key) {
 95         boolean flag = false;
 96         try {
 97             cacheMap.remove(key);
 98             cacheConfMap.remove(key);
 99             flag = true;
100         } catch (Exception e) {
101             e.printStackTrace();
102         }
103         return flag;
104     }
105 
106     /**
107      * 清除缓存的线程
108      */
109     private static class ClearCache extends Thread {
110         public void run() {
111             while (true) {
112                 // 记录要清除的key
113                 Set<Object> tempSet = new HashSet<>();
114                 Set<Object> set = cacheConfMap.keySet();
115                 Iterator<Object> it = set.iterator();
116                 while (it.hasNext()) {
117                     Object key = it.next();
118                     CacheConfModel ccm = (CacheConfModel) cacheConfMap.get(key);
119                     // 比较是否需要清除
120                     if (!ccm.isForever()) {
121                         if ((new Date().getTime() - ccm.getBeginTime()) >= ccm.getDurableTime() * 1000L) {
122                             // 可以清除,先记录下来
123                             tempSet.add(key);
124                         }
125                     }
126                 }
127                 // 真正清除
128                 Iterator<Object> tempIt = tempSet.iterator();
129                 while (tempIt.hasNext()) {
130                     Object key = tempIt.next();
131                     cacheMap.remove(key);
132                     cacheConfMap.remove(key);
133                 }
134                 
135                 LocalDateTime localDateTime = LocalDateTime.now();  
136                 System.out.println("当前时间为:" + localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + ",缓存大小==>" + cacheMap.size());
137                 // 线程休息
138                 try {
139                     Thread.sleep(60 * 10 * 1000L);
140                 } catch (InterruptedException e) {
141                     e.printStackTrace();
142                 }
143             }
144         }
145     }
146 
147 }

缓存配置实体类

 1 package com.wbproject.util.cache;
 2 
 3 /**
 4  * 缓存配置实体类
 5  *
 6  * @author wangbo
 7  * @date 2018-03-07 12:42:56
 8  */
 9 public class CacheConfModel implements java.io.Serializable {
10 
11     private static final long serialVersionUID = 1L;
12 
13     private long beginTime;// 缓存开始时间
14 
15     private int durableTime;// 持续时间,秒
16 
17     private boolean isForever = false;// 是否持久
18 
19     public long getBeginTime() {
20         return beginTime;
21     }
22 
23     public void setBeginTime(long beginTime) {
24         this.beginTime = beginTime;
25     }
26 
27     public boolean isForever() {
28         return isForever;
29     }
30 
31     public void setForever(boolean isForever) {
32         this.isForever = isForever;
33     }
34 
35     public int getDurableTime() {
36         return durableTime;
37     }
38 
39     public void setDurableTime(int durableTime) {
40         this.durableTime = durableTime;
41     }
42 
43 }

最后贴上操作缓存的工具类,简单写了几个用到的,如果没有可自行添加:

 1 package com.wbproject.util.cache;
 2 
 3 import java.util.Date;
 4 
 5 /**
 6  * 操作缓存的工具类
 7  *
 8  * @author wangbo
 9  * @date 2018-03-07 13:01:54
10  */
11 public class CacheUtil {
12 
13     /**
14      * 添加缓存
15      * 
16      * @param key
17      * @param value
18      */
19     public static boolean addCache(Object key, Object value) {
20         CacheManage cm = CacheManage.getInstance();
21         CacheConfModel cModel = new CacheConfModel();
22         cModel.setForever(true);
23         return cm.addCache(key, value, cModel);
24     }
25 
26     /**
27      * 添加临时缓存
28      * 
29      * @param key
30      * @param value
31      * @param durableTime
32      */
33     public static boolean addTempCache(Object key, Object value, int durableTime) {
34         CacheManage cm = CacheManage.getInstance();
35         CacheConfModel cModel = new CacheConfModel();
36         cModel.setBeginTime(new Date().getTime());
37         cModel.setDurableTime(durableTime);
38         cModel.setForever(false);
39         return cm.addCache(key, value, cModel);
40     }
41 
42     /**
43      * 获取缓存
44      * 
45      * @param key
46      * @return
47      */
48     public static Object getValue(Object key) {
49         CacheManage cm = CacheManage.getInstance();
50         Object ob = cm.getValue(key);
51         if (ob == null) {
52             return null;
53         }
54         return ob;
55     }
56 
57     /**
58      * 获取字符串缓存
59      * 
60      * @param key
61      * @return
62      */
63     public static String getStringValue(Object key) {
64         CacheManage cm = CacheManage.getInstance();
65         Object ob = cm.getValue(key);
66         if (ob == null) {
67             return null;
68         }
69         return ob.toString();
70     }
71 
72 }

 

posted @ 2018-03-09 14:56  一线大码  Views(924)  Comments(0Edit  收藏  举报