Map作为缓存使用

  1 public class MapCache {
  2 
  3     /**
  4      * 默认存储1024个缓存
  5      */
  6     private static final int DEFAULT_CACHES = 1024;
  7 
  8     private static final MapCache INS = new MapCache();
  9 
 10     public static MapCache single() {
 11         return INS;
 12     }
 13 
 14     /**
 15      * 缓存容器
 16      */
 17     private static Map<String, CacheObject> cachePool;
 18 
 19     public MapCache() {
 20         this(DEFAULT_CACHES);
 21     }
 22 
 23     public MapCache(int cacheCount) {
 24         cachePool = new ConcurrentHashMap(cacheCount);
 25     }
 26 
 27     /**
 28      * 读取一个缓存
 29      *
 30      * @param key 缓存key
 31      * @param <T>
 32      * @return
 33      */
 34     public static <T> T get(String key) {
 35         CacheObject cacheObject = cachePool.get(key);
 36         if (null != cacheObject) {
 37             long cur = System.currentTimeMillis() / 1000;
 38             if (cacheObject.getExpired() <= 0 || cacheObject.getExpired() > cur) {
 39                 Object result = cacheObject.getValue();
 40                 return (T) result;
 41             }
 42         }
 43         return null;
 44     }
 45 
 46     /**
 47      * 读取一个hash类型缓存
 48      *
 49      * @param key   缓存key
 50      * @param field 缓存field
 51      * @param <T>
 52      * @return
 53      */
 54     public <T> T hget(String key, String field) {
 55         key = key + ":" + field;
 56         return this.get(key);
 57     }
 58 
 59     /**
 60      * 设置一个缓存
 61      *
 62      * @param key   缓存key
 63      * @param value 缓存value
 64      */
 65     public static void set(String key, Object value) {
 66         INS.set(key, value, -1);
 67     }
 68 
 69     /**
 70      * 设置一个缓存并带过期时间
 71      *
 72      * @param key     缓存key
 73      * @param value   缓存value
 74      * @param expired 过期时间,单位为秒
 75      */
 76     public void set(String key, Object value, long expired) {
 77         expired = expired > 0 ? System.currentTimeMillis() / 1000 + expired : expired;
 78         CacheObject cacheObject = new CacheObject(key, value, expired);
 79         cachePool.put(key, cacheObject);
 80     }
 81 
 82     /**
 83      * 设置一个hash缓存
 84      *
 85      * @param key   缓存key
 86      * @param field 缓存field
 87      * @param value 缓存value
 88      */
 89     public void hset(String key, String field, Object value) {
 90         this.hset(key, field, value, -1);
 91     }
 92 
 93     /**
 94      * 设置一个hash缓存并带过期时间
 95      *
 96      * @param key     缓存key
 97      * @param field   缓存field
 98      * @param value   缓存value
 99      * @param expired 过期时间,单位为秒
100      */
101     public void hset(String key, String field, Object value, long expired) {
102         key = key + ":" + field;
103         expired = expired > 0 ? System.currentTimeMillis() / 1000 + expired : expired;
104         CacheObject cacheObject = new CacheObject(key, value, expired);
105         cachePool.put(key, cacheObject);
106     }
107 
108     /**
109      * 根据key删除缓存
110      *
111      * @param key 缓存key
112      */
113     public static void del(String key) {
114         cachePool.remove(key);
115     }
116 
117     /**
118      * 根据key和field删除缓存
119      *
120      * @param key   缓存key
121      * @param field 缓存field
122      */
123     public void hdel(String key, String field) {
124         key = key + ":" + field;
125         this.del(key);
126     }
127 
128     /**
129      * 清空缓存
130      */
131     public void clean() {
132         cachePool.clear();
133     }
134 
135     static class CacheObject {
136         private String key;
137         private Object value;
138         private long expired;
139 
140         public CacheObject(String key, Object value, long expired) {
141             this.key = key;
142             this.value = value;
143             this.expired = expired;
144         }
145 
146         public String getKey() {
147             return key;
148         }
149 
150         public Object getValue() {
151             return value;
152         }
153 
154         public long getExpired() {
155             return expired;
156         }
157     }
158 }

转载处:https://blog.csdn.net/xsj_blog/article/details/83056143

posted @ 2019-12-18 09:02  danRiver  阅读(1897)  评论(0编辑  收藏  举报