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