CacheSource增加setnxex系列方法

This commit is contained in:
Redkale
2023-01-15 16:21:37 +08:00
parent 55ddf7c417
commit 8789c0915b
2 changed files with 90 additions and 0 deletions

View File

@@ -801,6 +801,31 @@ public final class CacheMemorySource extends AbstractCacheSource {
return setnx(CacheEntryType.OBJECT, key, value);
}
@Override
public <T> boolean setnxex(String key, int expireSeconds, Type type, T value) {
return setnxex(CacheEntryType.OBJECT, expireSeconds, key, value);
}
@Override
public <T> boolean setnxex(String key, int expireSeconds, Convert convert, Type type, T value) {
return setnxex(CacheEntryType.OBJECT, expireSeconds, key, value);
}
@Override
public boolean setnxexBytes(final String key, final int expireSeconds, final byte[] value) {
return setnxex(CacheEntryType.BYTES, expireSeconds, key, value);
}
@Override
public boolean setnxexString(String key, int expireSeconds, String value) {
return setnxex(CacheEntryType.STRING, expireSeconds, key, value);
}
@Override
public boolean setnxexLong(String key, int expireSeconds, long value) {
return setnxex(CacheEntryType.LONG, expireSeconds, key, value);
}
@Override
public <T> T getSet(String key, Type type, T value) {
T old = get(key, type);
@@ -917,6 +942,24 @@ public final class CacheMemorySource extends AbstractCacheSource {
}
}
protected boolean setnxex(CacheEntryType cacheType, int expireSeconds, String key, Object value) {
if (key == null) {
return false;
}
CacheEntry entry = container.get(key);
if (entry == null) {
entry = new CacheEntry(cacheType, expireSeconds, key, value, null, null, null);
container.putIfAbsent(key, entry);
return true;
} else {
if (expireSeconds > 0) {
entry.expireSeconds = expireSeconds;
}
entry.lastAccessed = (int) (System.currentTimeMillis() / 1000);
return false;
}
}
@Override
public <T> void setex(String key, int expireSeconds, Type type, T value) {
set(CacheEntryType.OBJECT, expireSeconds, key, value);
@@ -957,6 +1000,31 @@ public final class CacheMemorySource extends AbstractCacheSource {
return CompletableFuture.runAsync(() -> setexLong(key, expireSeconds, value), getExecutor()).whenComplete(futureCompleteConsumer);
}
@Override
public CompletableFuture<Boolean> setnxexStringAsync(String key, int expireSeconds, String value) {
return CompletableFuture.supplyAsync(() -> setnxexString(key, expireSeconds, value), getExecutor()).whenComplete(futureCompleteConsumer);
}
@Override
public CompletableFuture<Boolean> setnxexLongAsync(String key, int expireSeconds, long value) {
return CompletableFuture.supplyAsync(() -> setnxexLong(key, expireSeconds, value), getExecutor()).whenComplete(futureCompleteConsumer);
}
@Override
public CompletableFuture<Boolean> setnxexBytesAsync(String key, int expireSeconds, byte[] value) {
return CompletableFuture.supplyAsync(() -> setnxexBytes(key, expireSeconds, value), getExecutor()).whenComplete(futureCompleteConsumer);
}
@Override
public <T> CompletableFuture<Boolean> setnxexAsync(final String key, final int expireSeconds, final Type type, final T value) {
return CompletableFuture.supplyAsync(() -> setnxex(key, expireSeconds, type, value), getExecutor()).whenComplete(futureCompleteConsumer);
}
@Override
public <T> CompletableFuture<Boolean> setnxexAsync(final String key, final int expireSeconds, final Convert convert, final Type type, final T value) {
return CompletableFuture.supplyAsync(() -> setnxex(key, expireSeconds, convert, type, value), getExecutor()).whenComplete(futureCompleteConsumer);
}
@Override
public void expire(String key, int expireSeconds) {
if (key == null) {

View File

@@ -135,6 +135,17 @@ public interface CacheSource extends Resourcable {
public boolean setnxBytes(final String key, final byte[] value);
//------------------------ setnxex ------------------------
public <T> boolean setnxex(final String key, final int expireSeconds, final Type type, final T value);
public <T> boolean setnxex(final String key, final int expireSeconds, final Convert convert, final Type type, final T value);
public boolean setnxexString(final String key, final int expireSeconds, final String value);
public boolean setnxexLong(final String key, final int expireSeconds, final long value);
public boolean setnxexBytes(final String key, final int expireSeconds, final byte[] value);
//------------------------ setex ------------------------
public <T> void setex(final String key, final int expireSeconds, final Type type, final T value);
@@ -440,6 +451,17 @@ public interface CacheSource extends Resourcable {
public CompletableFuture<Boolean> setnxBytesAsync(final String key, final byte[] value);
//------------------------ setnxexAsync ------------------------
public <T> CompletableFuture<Boolean> setnxexAsync(final String key, final int expireSeconds, final Type type, final T value);
public <T> CompletableFuture<Boolean> setnxexAsync(final String key, final int expireSeconds, final Convert convert, final Type type, final T value);
public CompletableFuture<Boolean> setnxexStringAsync(final String key, final int expireSeconds, final String value);
public CompletableFuture<Boolean> setnxexLongAsync(final String key, final int expireSeconds, final long value);
public CompletableFuture<Boolean> setnxexBytesAsync(final String key, final int expireSeconds, final byte[] value);
//------------------------ setexAsync ------------------------
public <T> CompletableFuture<Void> setexAsync(final String key, final int expireSeconds, final Type type, final T value);