|
6 | 6 | import lombok.extern.slf4j.Slf4j; |
7 | 7 | import org.springframework.cache.support.AbstractValueAdaptingCache; |
8 | 8 | import org.springframework.data.redis.core.RedisTemplate; |
| 9 | +import org.springframework.util.CollectionUtils; |
9 | 10 | import org.springframework.util.StringUtils; |
10 | 11 |
|
11 | 12 | import java.util.Map; |
@@ -64,12 +65,11 @@ public <T> T get(Object key, Callable<T> valueLoader) { |
64 | 65 | return (T) value; |
65 | 66 | } |
66 | 67 |
|
67 | | - ReentrantLock lock = keyLockMap.get(key.toString()); |
68 | | - if (lock == null) { |
69 | | - log.debug("create lock for key : {}", key); |
70 | | - lock = new ReentrantLock(); |
71 | | - keyLockMap.putIfAbsent(key.toString(), lock); |
72 | | - } |
| 68 | + ReentrantLock lock = keyLockMap.computeIfAbsent(key.toString(), s -> { |
| 69 | + log.trace("create lock for key : {}", s); |
| 70 | + return new ReentrantLock(); |
| 71 | + }); |
| 72 | + |
73 | 73 | try { |
74 | 74 | lock.lock(); |
75 | 75 | value = lookup(key); |
@@ -147,8 +147,9 @@ public void evict(Object key) { |
147 | 147 | public void clear() { |
148 | 148 | // 先清除redis中缓存数据,然后清除caffeine中的缓存,避免短时间内如果先清除caffeine缓存后其他请求会再从redis里加载到caffeine中 |
149 | 149 | Set<Object> keys = stringKeyRedisTemplate.keys(this.name.concat(":*")); |
150 | | - for (Object key : keys) { |
151 | | - stringKeyRedisTemplate.delete(key); |
| 150 | + |
| 151 | + if (!CollectionUtils.isEmpty(keys)){ |
| 152 | + stringKeyRedisTemplate.delete(keys); |
152 | 153 | } |
153 | 154 |
|
154 | 155 | push(new CacheMessage(this.name, null)); |
@@ -180,9 +181,8 @@ private Object getKey(Object key) { |
180 | 181 | } |
181 | 182 |
|
182 | 183 | private long getExpire() { |
183 | | - long expire = defaultExpiration; |
184 | 184 | Long cacheNameExpire = expires.get(this.name); |
185 | | - return cacheNameExpire == null ? expire : cacheNameExpire.longValue(); |
| 185 | + return cacheNameExpire == null ? defaultExpiration : cacheNameExpire; |
186 | 186 | } |
187 | 187 |
|
188 | 188 | /** |
|
0 commit comments