CacheSource增加msetnx接口

This commit is contained in:
redkale
2023-12-22 14:54:49 +08:00
parent ce36250e60
commit 88c20f798f
2 changed files with 57 additions and 6 deletions

View File

@@ -25,7 +25,6 @@ import org.redkale.convert.json.*;
import org.redkale.inject.ResourceEvent;
import org.redkale.service.Local;
import org.redkale.util.*;
import org.redkale.annotation.ResourceChanged;
/**
* CacheSource的默认实现--内存缓存
@@ -300,21 +299,31 @@ public final class CacheMemorySource extends AbstractCacheSource {
}
}
@Override
public CompletableFuture<Void> msetAsync(Serializable... keyVals) {
return runFuture(() -> mset(keyVals));
}
@Override
public void mset(Map map) {
map.forEach((key, val) -> set0(key.toString(), 0, null, null, val));
}
@Override
public CompletableFuture<Void> msetAsync(Serializable... keyVals) {
return runFuture(() -> mset(keyVals));
}
@Override
public CompletableFuture<Void> msetnxAsync(Serializable... keyVals) {
return runFuture(() -> msetnx(keyVals));
}
@Override
public CompletableFuture<Void> msetAsync(Map map) {
return runFuture(() -> mset(map));
}
@Override
public CompletableFuture<Void> msetnxAsync(Map map) {
return runFuture(() -> msetnx(map));
}
@Override
public <T> void set(String key, Convert convert, Type type, T value) {
set0(key, 0, convert, type, value);
@@ -420,6 +429,34 @@ public final class CacheMemorySource extends AbstractCacheSource {
}
}
@Override
public void msetnx(Serializable... keyVals) {
if (keyVals.length % 2 != 0) {
throw new SourceException("key value must be paired");
}
msetnx(Utility.ofMap(keyVals));
}
@Override
public void msetnx(Map map) {
containerLock.lock();
try {
for (Object key : map.keySet()) {
if (find(key.toString(), CacheEntryType.OBJECT) != null) {
return;
}
}
for (Map.Entry<String, Object> en : (Set<Map.Entry<String, Object>>) map.entrySet()) {
CacheEntry entry = new CacheEntry(CacheEntryType.OBJECT, en.getKey());
container.put(en.getKey(), entry);
entry.setObjectValue(this.convert, null, en.getValue());
entry.lastAccessed = System.currentTimeMillis();
}
} finally {
containerLock.unlock();
}
}
@Override
public <T> void setex(String key, int expireSeconds, Convert convert, Type type, T value) {
set0(key, expireSeconds, convert, type, value);

View File

@@ -171,6 +171,15 @@ public interface CacheSource extends Resourcable {
msetAsync(map).join();
}
//MSETNX key value [key value ...]
default void msetnx(Serializable... keyVals) {
msetnxAsync(keyVals).join();
}
default void msetnx(Map map) {
msetnxAsync(map).join();
}
//------------------------ setnx ------------------------
default <T> boolean setnx(String key, Convert convert, Type type, T value) {
return setnxAsync(key, convert, type, value).join();
@@ -1057,6 +1066,11 @@ public interface CacheSource extends Resourcable {
public CompletableFuture<Void> msetAsync(Map map);
//MSET key value [key value ...]
public CompletableFuture<Void> msetnxAsync(Serializable... keyVals);
public CompletableFuture<Void> msetnxAsync(Map map);
//------------------------ setnx ------------------------
public <T> CompletableFuture<Boolean> setnxAsync(String key, Convert convert, Type type, T value);