前言
最近項目需求需要一個類似於redis可以設置過期時間的K,V存儲方式。項目前期暫時不引進redis,暫時用java內存代替。
解決方案
1. ExpiringMap
功能簡介 :
1.可設置Map中的Entry在一段時間后自動過期。 2.可設置Map最大容納值,當到達Maximum size后,再次插入值會導致Map中的第一個值過期。 3.可添加監聽事件,在監聽到Entry過期時調度監聽函數。 4.可以設置懶加載,在調用get()方法時創建對象。
github地址:https://github.com/jhalterman/expiringmap/
maven添加依賴即可使用
<dependency> <groupId>net.jodah</groupId> <artifactId>expiringmap</artifactId> <version>0.5.8</version> </dependency>
public static void main(String[] args) throws InterruptedException { ExpiringMap<String,String> map = ExpiringMap.builder() .maxSize(100) .expiration(1, TimeUnit.SECONDS) .expirationPolicy(ExpirationPolicy.ACCESSED) .variableExpiration() .build(); map.put("test","test123"); Thread.sleep(500); String test= map.get("test"); System.err.println(test); }
2.Guava - LoadingCache
Google開源出來的一個線程安全的本地緩存解決方案。
特點:提供緩存回收機制,監控緩存加載/命中情況,靈活強大的功能,簡單易上手的api
但是該cache不會在特定時間准時回收鍵值,所以不適用於我當前的業務場景。
詳細描述介紹看我的另外一篇博客:https://www.cnblogs.com/xhq1024/p/11174775.html
<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>27.1-jre</version> </dependency>
3. ExpiryMap
這是網上某位大佬自己封裝的map,繼承至HashMap,重寫了所有對外的方法,對每個key值都設置了有效期。
我在其基礎上增加了使用單例模式獲取map。
1 import java.util.*; 2 3 /** 4 * @Title: ExpiryMap 可以設置過期時間的Map 5 * @description ExpiryMap繼承至HashMap 重寫了所有對外的方法,對每個key值都設置了有效期 6 * @Author: xx 7 * @Version: 1.0 8 */ 9 public class ExpiryMap<K, V> extends HashMap<K, V> { 10 11 private static final long serialVersionUID = 1L; 12 13 /** 14 * default expiry time 2s 15 */ 16 private long EXPIRY = 1000 * 2; 17 18 private HashMap<K, Long> expiryMap = new HashMap<>(); 19 20 /** 緩存實例對象 */ 21 private volatile static ExpiryMap<String, String> SameUrlMap; 22 23 /** 24 * 采用單例模式獲取實例 25 * @return 26 */ 27 public static ExpiryMap getInstance() { 28 //第一次判空,提高效率 29 if (null == SameUrlMap) { 30 //保證線程安全 31 synchronized (ExpiryMap.class) { 32 //第二次判空,保證單例對象的唯一性,防止第一次有多個線程進入第一個if判斷 33 if (null == SameUrlMap) { 34 SameUrlMap = new ExpiryMap<>(); 35 } 36 } 37 } 38 return SameUrlMap; 39 } 40 41 public ExpiryMap(){ 42 super(); 43 } 44 45 public ExpiryMap(long defaultExpiryTime){ 46 this(1 << 4, defaultExpiryTime); 47 } 48 49 public ExpiryMap(int initialCapacity, long defaultExpiryTime){ 50 super(initialCapacity); 51 this.EXPIRY = defaultExpiryTime; 52 } 53 54 @Override 55 public V put(K key, V value) { 56 expiryMap.put(key, System.currentTimeMillis() + EXPIRY); 57 return super.put(key, value); 58 } 59 60 @Override 61 public boolean containsKey(Object key) { 62 return !checkExpiry(key, true) && super.containsKey(key); 63 } 64 /** 65 * @param key 66 * @param value 67 * @param expiryTime 鍵值對有效期 毫秒 68 * @return 69 */ 70 public V put(K key, V value, long expiryTime) { 71 expiryMap.put(key, System.currentTimeMillis() + expiryTime); 72 return super.put(key, value); 73 } 74 75 @Override 76 public int size() { 77 return entrySet().size(); 78 } 79 80 @Override 81 public boolean isEmpty() { 82 return entrySet().size() == 0; 83 } 84 85 @Override 86 public boolean containsValue(Object value) { 87 if (value == null) { 88 return Boolean.FALSE; 89 } 90 Set<Entry<K, V>> set = super.entrySet(); 91 Iterator<Entry<K, V>> iterator = set.iterator(); 92 while (iterator.hasNext()) { 93 java.util.Map.Entry<K, V> entry = iterator.next(); 94 if(value.equals(entry.getValue())){ 95 if(checkExpiry(entry.getKey(), false)) { 96 iterator.remove(); 97 return Boolean.FALSE; 98 }else { 99 return Boolean.TRUE; 100 } 101 } 102 } 103 return Boolean.FALSE; 104 } 105 106 @Override 107 public Collection<V> values() { 108 109 Collection<V> values = super.values(); 110 111 if(values == null || values.size() < 1) { 112 return values; 113 } 114 115 Iterator<V> iterator = values.iterator(); 116 117 while (iterator.hasNext()) { 118 V next = iterator.next(); 119 if(!containsValue(next)) { 120 iterator.remove(); 121 } 122 } 123 return values; 124 } 125 126 @Override 127 public V get(Object key) { 128 if (key == null) { 129 return null; 130 } 131 if(checkExpiry(key, true)) { 132 return null; 133 } 134 return super.get(key); 135 } 136 /** 137 * 138 * @Description: 是否過期 139 * @param key 140 * @return null:不存在或key為null -1:過期 存在且沒過期返回value 因為過期的不是實時刪除,所以稍微有點作用 141 */ 142 public Object isInvalid(Object key) { 143 if (key == null) { 144 return null; 145 } 146 if(!expiryMap.containsKey(key)){ 147 return null; 148 } 149 long expiryTime = expiryMap.get(key); 150 151 boolean flag = System.currentTimeMillis() > expiryTime; 152 153 if(flag){ 154 super.remove(key); 155 expiryMap.remove(key); 156 return -1; 157 } 158 return super.get(key); 159 } 160 161 @Override 162 public void putAll(Map<? extends K, ? extends V> m) { 163 for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) { 164 expiryMap.put(e.getKey(), System.currentTimeMillis() + EXPIRY); 165 } 166 super.putAll(m); 167 } 168 169 @Override 170 public Set<Map.Entry<K,V>> entrySet() { 171 Set<java.util.Map.Entry<K, V>> set = super.entrySet(); 172 Iterator<java.util.Map.Entry<K, V>> iterator = set.iterator(); 173 while (iterator.hasNext()) { 174 java.util.Map.Entry<K, V> entry = iterator.next(); 175 if(checkExpiry(entry.getKey(), false)) { 176 iterator.remove(); 177 } 178 } 179 180 return set; 181 } 182 /** 183 * 184 * @Description: 是否過期 185 * @param expiryTime true 過期 186 * @param isRemoveSuper true super刪除 187 * @return 188 */ 189 private boolean checkExpiry(Object key, boolean isRemoveSuper){ 190 191 if(!expiryMap.containsKey(key)){ 192 return Boolean.FALSE; 193 } 194 long expiryTime = expiryMap.get(key); 195 196 boolean flag = System.currentTimeMillis() > expiryTime; 197 198 if(flag){ 199 if(isRemoveSuper) { 200 super.remove(key); 201 } 202 expiryMap.remove(key); 203 } 204 return flag; 205 } 206 207 public static void main(String[] args) throws InterruptedException { 208 ExpiryMap<String, String> map = new ExpiryMap<>(); 209 map.put("test", "xxx"); 210 map.put("test2", "ankang", 5000); 211 System.out.println("test==" + map.get("test")); 212 Thread.sleep(3000); 213 System.out.println("test==" + map.get("test")); 214 System.out.println("test2==" + map.get("test2")); 215 Thread.sleep(3000); 216 System.out.println("test2==" + map.get("test2")); 217 } 218 }
附上ExpiryMap原文地址:https://blog.csdn.net/u011534095/article/details/54091337