CacheSource

This commit is contained in:
redkale
2023-12-25 12:25:20 +08:00
parent 08f679c422
commit 0c18e29aaf
2 changed files with 15 additions and 14 deletions

View File

@@ -310,8 +310,8 @@ public final class CacheMemorySource extends AbstractCacheSource {
} }
@Override @Override
public CompletableFuture<Void> msetnxAsync(Serializable... keyVals) { public CompletableFuture<Boolean> msetnxAsync(Serializable... keyVals) {
return runFuture(() -> msetnx(keyVals)); return supplyFuture(() -> msetnx(keyVals));
} }
@Override @Override
@@ -320,8 +320,8 @@ public final class CacheMemorySource extends AbstractCacheSource {
} }
@Override @Override
public CompletableFuture<Void> msetnxAsync(Map map) { public CompletableFuture<Boolean> msetnxAsync(Map map) {
return runFuture(() -> msetnx(map)); return supplyFuture(() -> msetnx(map));
} }
@Override @Override
@@ -430,20 +430,20 @@ public final class CacheMemorySource extends AbstractCacheSource {
} }
@Override @Override
public void msetnx(Serializable... keyVals) { public boolean msetnx(Serializable... keyVals) {
if (keyVals.length % 2 != 0) { if (keyVals.length % 2 != 0) {
throw new SourceException("key value must be paired"); throw new SourceException("key value must be paired");
} }
msetnx(Utility.ofMap(keyVals)); return msetnx(Utility.ofMap(keyVals));
} }
@Override @Override
public void msetnx(Map map) { public boolean msetnx(Map map) {
containerLock.lock(); containerLock.lock();
try { try {
for (Object key : map.keySet()) { for (Object key : map.keySet()) {
if (find(key.toString(), CacheEntryType.OBJECT) != null) { if (find(key.toString(), CacheEntryType.OBJECT) != null) {
return; return false;
} }
} }
for (Map.Entry<String, Object> en : (Set<Map.Entry<String, Object>>) map.entrySet()) { for (Map.Entry<String, Object> en : (Set<Map.Entry<String, Object>>) map.entrySet()) {
@@ -452,6 +452,7 @@ public final class CacheMemorySource extends AbstractCacheSource {
entry.setObjectValue(this.convert, null, en.getValue()); entry.setObjectValue(this.convert, null, en.getValue());
entry.lastAccessed = System.currentTimeMillis(); entry.lastAccessed = System.currentTimeMillis();
} }
return true;
} finally { } finally {
containerLock.unlock(); containerLock.unlock();
} }

View File

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