mirror of https://github.com/halo-dev/halo
Add putIfAbsent method in CacheStore
parent
5846d5abb9
commit
58bf3c1d2a
|
@ -3,6 +3,7 @@ package cc.ryanc.halo.cache;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.time.DateUtils;
|
import org.apache.commons.lang3.time.DateUtils;
|
||||||
import org.springframework.lang.NonNull;
|
import org.springframework.lang.NonNull;
|
||||||
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
@ -35,6 +36,15 @@ public abstract class AbstractCacheStore<K, V> implements CacheStore<K, V> {
|
||||||
*/
|
*/
|
||||||
abstract void putInternal(@NonNull K key, @NonNull CacheWrapper<V> cacheWrapper);
|
abstract void putInternal(@NonNull K key, @NonNull CacheWrapper<V> cacheWrapper);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Puts the cache wrapper if the key is absent.
|
||||||
|
*
|
||||||
|
* @param key key must not be null
|
||||||
|
* @param cacheWrapper cache wrapper must not be null
|
||||||
|
* @return true if the key is absent and the value is set, false if the key is present before, or null if any other reason
|
||||||
|
*/
|
||||||
|
abstract Boolean putInternalIfAbsent(@NonNull K key, @NonNull CacheWrapper<V> cacheWrapper);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<V> get(K key) {
|
public Optional<V> get(K key) {
|
||||||
Assert.notNull(key, "Cache key must not be blank");
|
Assert.notNull(key, "Cache key must not be blank");
|
||||||
|
@ -60,20 +70,47 @@ public abstract class AbstractCacheStore<K, V> implements CacheStore<K, V> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void put(K key, V value, long timeout, TimeUnit timeUnit) {
|
public void put(K key, V value, long timeout, TimeUnit timeUnit) {
|
||||||
Assert.notNull(key, "Cache key must not be blank");
|
putInternal(key, buildCacheWrapper(value, timeout, timeUnit));
|
||||||
Assert.notNull(value, "Cache value must not be null");
|
}
|
||||||
Assert.isTrue(timeout > 0, "Cache expiration timeout must not be less than 1");
|
|
||||||
Assert.notNull(timeUnit, "Time unit must not be null");
|
@Override
|
||||||
|
public Boolean putIfAbsent(K key, V value, long timeout, TimeUnit timeUnit) {
|
||||||
|
return putInternalIfAbsent(key, buildCacheWrapper(value, timeout, timeUnit));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void put(K key, V value) {
|
||||||
|
putInternal(key, buildCacheWrapper(value, 0, null));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds cache wrapper.
|
||||||
|
*
|
||||||
|
* @param value cache value must not be null
|
||||||
|
* @param timeout the key expiry time, if the expiry time is less than 1, the cache won't be expired
|
||||||
|
* @param timeUnit timeout unit must
|
||||||
|
* @return cache wrapper
|
||||||
|
*/
|
||||||
|
@NonNull
|
||||||
|
private CacheWrapper<V> buildCacheWrapper(@NonNull V value, long timeout, @Nullable TimeUnit timeUnit) {
|
||||||
|
Assert.notNull(value, "Cache value must not be null");
|
||||||
|
Assert.isTrue(timeout >= 0, "Cache expiration timeout must not be less than 1");
|
||||||
|
|
||||||
// Handle expiration
|
|
||||||
Date now = cc.ryanc.halo.utils.DateUtils.now();
|
Date now = cc.ryanc.halo.utils.DateUtils.now();
|
||||||
|
|
||||||
long millis = timeUnit.toMillis(timeout);
|
Date expireAt = null;
|
||||||
if (millis <= 0) {
|
|
||||||
millis = 1L;
|
if (timeout > 0 && timeUnit != null) {
|
||||||
|
// Handle expiration
|
||||||
|
long millis = timeUnit.toMillis(timeout);
|
||||||
|
if (millis <= 0) {
|
||||||
|
millis = 1L;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calc the expiry time
|
||||||
|
expireAt = DateUtils.addMilliseconds(now, Long.valueOf(millis).intValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
Date expireAt = DateUtils.addMilliseconds(now, Long.valueOf(millis).intValue());
|
|
||||||
|
|
||||||
// Build cache wrapper
|
// Build cache wrapper
|
||||||
CacheWrapper<V> cacheWrapper = new CacheWrapper<>();
|
CacheWrapper<V> cacheWrapper = new CacheWrapper<>();
|
||||||
|
@ -81,24 +118,6 @@ public abstract class AbstractCacheStore<K, V> implements CacheStore<K, V> {
|
||||||
cacheWrapper.setExpireAt(expireAt);
|
cacheWrapper.setExpireAt(expireAt);
|
||||||
cacheWrapper.setData(value);
|
cacheWrapper.setData(value);
|
||||||
|
|
||||||
putInternal(key, cacheWrapper);
|
return cacheWrapper;
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void put(K key, V value) {
|
|
||||||
Assert.notNull(key, "Cache key must not be blank");
|
|
||||||
Assert.notNull(value, "Cache value must not be null");
|
|
||||||
|
|
||||||
// Get current time
|
|
||||||
Date now = cc.ryanc.halo.utils.DateUtils.now();
|
|
||||||
|
|
||||||
// Build cache wrapper
|
|
||||||
CacheWrapper<V> cacheWrapper = new CacheWrapper<>();
|
|
||||||
cacheWrapper.setCreateAt(now);
|
|
||||||
cacheWrapper.setExpireAt(null);
|
|
||||||
cacheWrapper.setData(value);
|
|
||||||
|
|
||||||
putInternal(key, cacheWrapper);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,6 +34,17 @@ public interface CacheStore<K, V> {
|
||||||
*/
|
*/
|
||||||
void put(@NonNull K key, @NonNull V value, long timeout, @NonNull TimeUnit timeUnit);
|
void put(@NonNull K key, @NonNull V value, long timeout, @NonNull TimeUnit timeUnit);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Puts a cache which will be expired if the key is absent.
|
||||||
|
*
|
||||||
|
* @param key cache key must not be null
|
||||||
|
* @param value cache value must not be null
|
||||||
|
* @param timeout the key expiration must not be less than 1
|
||||||
|
* @param timeUnit timeout unit must not be null
|
||||||
|
* @return true if the key is absent and the value is set, false if the key is present before, or null if any other reason
|
||||||
|
*/
|
||||||
|
Boolean putIfAbsent(@NonNull K key, @NonNull V value, long timeout, @NonNull TimeUnit timeUnit);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Puts a non-expired cache.
|
* Puts a non-expired cache.
|
||||||
*
|
*
|
||||||
|
|
|
@ -37,6 +37,17 @@ public class InMemoryCacheStore extends StringCacheStore {
|
||||||
log.debug("Put cache wrapper: [{}]", putCacheWrapper);
|
log.debug("Put cache wrapper: [{}]", putCacheWrapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
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");
|
||||||
|
|
||||||
|
// Put the cache wrapper
|
||||||
|
CacheWrapper<String> putCacheWrapper = cacheContainer.putIfAbsent(key, cacheWrapper);
|
||||||
|
|
||||||
|
return cacheWrapper.equals(putCacheWrapper);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void delete(String key) {
|
public void delete(String key) {
|
||||||
Assert.hasText(key, "Cache key must not be blank");
|
Assert.hasText(key, "Cache key must not be blank");
|
||||||
|
|
Loading…
Reference in New Issue