mirror of https://github.com/halo-dev/halo
Replace synchronized with ReentrantLock
parent
ddf3c7a4fc
commit
0add4107ef
|
@ -5,6 +5,8 @@ import org.springframework.util.Assert;
|
|||
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
/**
|
||||
* In-memory cache store.
|
||||
|
@ -19,6 +21,11 @@ public class InMemoryCacheStore extends StringCacheStore {
|
|||
*/
|
||||
private final static ConcurrentHashMap<String, CacheWrapper<String>> cacheContainer = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* Lock.
|
||||
*/
|
||||
private Lock lock = new ReentrantLock();
|
||||
|
||||
@Override
|
||||
Optional<CacheWrapper<String>> getInternal(String key) {
|
||||
Assert.hasText(key, "Cache key must not be blank");
|
||||
|
@ -38,12 +45,14 @@ public class InMemoryCacheStore extends StringCacheStore {
|
|||
}
|
||||
|
||||
@Override
|
||||
synchronized Boolean putInternalIfAbsent(String key, CacheWrapper<String> cacheWrapper) {
|
||||
Boolean putInternalIfAbsent(String key, CacheWrapper<String> cacheWrapper) {
|
||||
Assert.hasText(key, "Cache key must not be blank");
|
||||
Assert.notNull(cacheWrapper, "Cache wrapper must not be null");
|
||||
|
||||
log.debug("Preparing to put key: [{}], value: [{}]", key, cacheWrapper);
|
||||
|
||||
try {
|
||||
lock.lock();
|
||||
// Get the value before
|
||||
Optional<String> valueOptional = get(key);
|
||||
|
||||
|
@ -54,10 +63,11 @@ public class InMemoryCacheStore extends StringCacheStore {
|
|||
|
||||
// Put the cache wrapper
|
||||
putInternal(key, cacheWrapper);
|
||||
|
||||
log.debug("Put successfully");
|
||||
|
||||
return true;
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
Loading…
Reference in New Issue