From 2daa9ce7fdcb404e3988cb0ad96f6db36148c87b Mon Sep 17 00:00:00 2001 From: redkale Date: Sun, 11 Jun 2023 17:30:12 +0800 Subject: [PATCH] =?UTF-8?q?CacheSource=E4=BC=98=E5=8C=96srem=E3=80=81sadd?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../net/http/WebSocketNodeService.java | 5 +- .../org/redkale/source/CacheMemorySource.java | 22 +-- .../java/org/redkale/source/CacheSource.java | 134 ++++++++++-------- 3 files changed, 90 insertions(+), 71 deletions(-) diff --git a/src/main/java/org/redkale/net/http/WebSocketNodeService.java b/src/main/java/org/redkale/net/http/WebSocketNodeService.java index 61f846bae..affaec629 100644 --- a/src/main/java/org/redkale/net/http/WebSocketNodeService.java +++ b/src/main/java/org/redkale/net/http/WebSocketNodeService.java @@ -5,8 +5,7 @@ import java.net.InetSocketAddress; import java.util.*; import java.util.concurrent.CompletableFuture; import java.util.logging.Level; -import org.redkale.annotation.AutoLoad; -import org.redkale.annotation.ResourceType; +import org.redkale.annotation.*; import static org.redkale.net.http.WebSocket.RETCODE_GROUP_EMPTY; import org.redkale.net.http.WebSocketNodeService; import org.redkale.service.*; @@ -122,7 +121,7 @@ public class WebSocketNodeService extends WebSocketNode implements Service { @Override public CompletableFuture disconnect(Serializable userid, WebSocketAddress wsaddr) { tryAcquireSemaphore(); - CompletableFuture future = source.sremAsync(WS_SOURCE_KEY_USERID_PREFIX + userid, WebSocketAddress.class, wsaddr); + CompletableFuture future = source.sremAsync(WS_SOURCE_KEY_USERID_PREFIX + userid, WebSocketAddress.class, wsaddr); if (semaphore != null) { future.whenComplete((r, e) -> releaseSemaphore()); } diff --git a/src/main/java/org/redkale/source/CacheMemorySource.java b/src/main/java/org/redkale/source/CacheMemorySource.java index d916e295c..7e1ee6676 100644 --- a/src/main/java/org/redkale/source/CacheMemorySource.java +++ b/src/main/java/org/redkale/source/CacheMemorySource.java @@ -1322,7 +1322,7 @@ public final class CacheMemorySource extends AbstractCacheSource { return list; } - protected void appendSetItem(CacheEntryType cacheType, String key, Object value) { + protected void appendSetItem(CacheEntryType cacheType, String key, List values) { if (key == null) { return; } @@ -1335,25 +1335,25 @@ public final class CacheMemorySource extends AbstractCacheSource { set = old.csetValue; } if (set != null) { - set.add(value); + set.addAll(values); } } else { - entry.csetValue.add(value); + entry.csetValue.addAll(values); } } @Override - public void sadd(String key, final Type componentType, T value) { - appendSetItem(CacheEntryType.OBJECT_SET, key, value); + public void sadd(String key, final Type componentType, T... values) { + appendSetItem(CacheEntryType.OBJECT_SET, key, List.of(values)); } @Override - public CompletableFuture saddAsync(final String key, final Type componentType, T value) { - return runAsync(() -> sadd(key, componentType, value), getExecutor()).whenComplete(futureCompleteConsumer); + public CompletableFuture saddAsync(final String key, final Type componentType, T... values) { + return runAsync(() -> sadd(key, componentType, values), getExecutor()).whenComplete(futureCompleteConsumer); } @Override - public int srem(String key, Type type, T value) { + public long srem(String key, Type type, T... values) { if (key == null) { return 0; } @@ -1361,12 +1361,12 @@ public final class CacheMemorySource extends AbstractCacheSource { if (entry == null || entry.csetValue == null) { return 0; } - return entry.csetValue.remove(value) ? 1 : 0; + return entry.csetValue.removeAll(List.of(values)) ? 1 : 0; } @Override - public CompletableFuture sremAsync(final String key, final Type componentType, final T value) { - return supplyAsync(() -> srem(key, componentType, value), getExecutor()).whenComplete(futureCompleteConsumer); + public CompletableFuture sremAsync(final String key, final Type componentType, final T... values) { + return supplyAsync(() -> srem(key, componentType, values), getExecutor()).whenComplete(futureCompleteConsumer); } @Override diff --git a/src/main/java/org/redkale/source/CacheSource.java b/src/main/java/org/redkale/source/CacheSource.java index 306999c92..f24953acc 100644 --- a/src/main/java/org/redkale/source/CacheSource.java +++ b/src/main/java/org/redkale/source/CacheSource.java @@ -456,24 +456,24 @@ public interface CacheSource extends Resourcable { return sismember(key, Long.class, value); } - public void sadd(final String key, final Type componentType, final T value); + public void sadd(final String key, final Type componentType, final T... values); - default void saddString(final String key, final String value) { - sadd(key, String.class, value); + default void saddString(final String key, final String... values) { + sadd(key, String.class, values); } - default void saddLong(final String key, final long value) { - sadd(key, Long.class, value); + default void saddLong(final String key, final Long... values) { + sadd(key, Long.class, values); } - public int srem(final String key, final Type componentType, final T value); + public long srem(final String key, final Type componentType, final T... values); - default int sremString(final String key, final String value) { - return srem(key, String.class, value); + default long sremString(final String key, final String... values) { + return srem(key, String.class, values); } - default int sremLong(final String key, final long value) { - return srem(key, Long.class, value); + default long sremLong(final String key, final Long... values) { + return srem(key, Long.class, values); } public T spop(final String key, final Type componentType); @@ -965,24 +965,24 @@ public interface CacheSource extends Resourcable { return sismemberAsync(key, Long.class, value); } - public CompletableFuture saddAsync(final String key, final Type componentType, final T value); + public CompletableFuture saddAsync(final String key, final Type componentType, final T... values); - default CompletableFuture saddStringAsync(final String key, final String value) { - return saddAsync(key, String.class, value); + default CompletableFuture saddStringAsync(final String key, final String... values) { + return saddAsync(key, String.class, values); } - default CompletableFuture saddLongAsync(final String key, final long value) { - return saddAsync(key, Long.class, value); + default CompletableFuture saddLongAsync(final String key, final Long... values) { + return saddAsync(key, Long.class, values); } - public CompletableFuture sremAsync(final String key, final Type componentType, final T value); + public CompletableFuture sremAsync(final String key, final Type componentType, final T... values); - default CompletableFuture sremStringAsync(final String key, final String value) { - return sremAsync(key, String.class, value); + default CompletableFuture sremStringAsync(final String key, final String... values) { + return sremAsync(key, String.class, values); } - default CompletableFuture sremLongAsync(final String key, final long value) { - return sremAsync(key, Long.class, value); + default CompletableFuture sremLongAsync(final String key, final Long... values) { + return sremAsync(key, Long.class, values); } public CompletableFuture spopAsync(final String key, final Type componentType); @@ -1050,37 +1050,7 @@ public interface CacheSource extends Resourcable { public CompletableFuture flushallAsync(); - //-------------------------- 过期方法 ---------------------------------- - @Deprecated(since = "2.8.0") - public Collection getCollection(final String key, final Type componentType); - - @Deprecated(since = "2.8.0") - public Map> getCollectionMap(final boolean set, final Type componentType, final String... keys); - - @Deprecated(since = "2.8.0") - public int getCollectionSize(final String key); - - @Deprecated(since = "2.8.0") - public Collection getexCollection(final String key, final int expireSeconds, final Type componentType); - - @Deprecated(since = "2.8.0") - public Map> getStringCollectionMap(final boolean set, final String... keys); - - @Deprecated(since = "2.8.0") - public Collection getStringCollection(final String key); - - @Deprecated(since = "2.8.0") - public Collection getexStringCollection(final String key, final int expireSeconds); - - @Deprecated(since = "2.8.0") - public Collection getLongCollection(final String key); - - @Deprecated(since = "2.8.0") - public Map> getLongCollectionMap(final boolean set, final String... keys); - - @Deprecated(since = "2.8.0") - public Collection getexLongCollection(final String key, final int expireSeconds); - + //-------------------------- 过期方法 ---------------------------------- @Deprecated(since = "2.8.0") public CompletableFuture> getCollectionAsync(final String key, final Type componentType); @@ -1238,7 +1208,7 @@ public interface CacheSource extends Resourcable { @Deprecated(since = "2.8.0") default int removeSetItem(final String key, final Type componentType, final T value) { - return srem(key, componentType, value); + return (int) srem(key, componentType, value); } @Deprecated(since = "2.8.0") @@ -1268,7 +1238,7 @@ public interface CacheSource extends Resourcable { @Deprecated(since = "2.8.0") default CompletableFuture removeSetItemAsync(final String key, final Type componentType, final T value) { - return sremAsync(key, componentType, value); + return sremAsync(key, componentType, value).thenApply(v -> v.intValue()); } @Deprecated(since = "2.8.0") @@ -1293,7 +1263,7 @@ public interface CacheSource extends Resourcable { @Deprecated(since = "2.8.0") default int removeStringSetItem(final String key, final String value) { - return sremString(key, value); + return (int) sremString(key, value); } @Deprecated(since = "2.8.0") @@ -1318,7 +1288,7 @@ public interface CacheSource extends Resourcable { @Deprecated(since = "2.8.0") default int removeLongSetItem(final String key, final long value) { - return sremLong(key, value); + return (int) sremLong(key, value); } @Deprecated(since = "2.8.0") @@ -1343,7 +1313,7 @@ public interface CacheSource extends Resourcable { @Deprecated(since = "2.8.0") default CompletableFuture removeStringSetItemAsync(final String key, final String value) { - return sremStringAsync(key, value); + return sremStringAsync(key, value).thenApply(v -> v.intValue()); } @Deprecated(since = "2.8.0") @@ -1368,7 +1338,7 @@ public interface CacheSource extends Resourcable { @Deprecated(since = "2.8.0") default CompletableFuture removeLongSetItemAsync(final String key, final long value) { - return sremLongAsync(key, value); + return sremLongAsync(key, value).thenApply(v -> v.intValue()); } @Deprecated(since = "2.8.0") @@ -1560,4 +1530,54 @@ public interface CacheSource extends Resourcable { default Map hmap(final String key, final Type type, int start, int limit) { return hscan(key, type, new AtomicLong(start), limit); } + + @Deprecated(since = "2.8.0") + default Collection getStringCollection(String key) { + return getStringCollectionAsync(key).join(); + } + + @Deprecated(since = "2.8.0") + default Map> getStringCollectionMap(final boolean set, String... keys) { + return getStringCollectionMapAsync(set, keys).join(); + } + + @Deprecated(since = "2.8.0") + default Collection getCollection(String key, final Type componentType) { + return (Collection) getCollectionAsync(key, componentType).join(); + } + + @Deprecated(since = "2.8.0") + default int getCollectionSize(String key) { + return getCollectionSizeAsync(key).join(); + } + + @Deprecated(since = "2.8.0") + default Collection getLongCollection(String key) { + return getLongCollectionAsync(key).join(); + } + + @Deprecated(since = "2.8.0") + default Map> getLongCollectionMap(final boolean set, String... keys) { + return getLongCollectionMapAsync(set, keys).join(); + } + + @Deprecated(since = "2.8.0") + default Collection getexCollection(String key, final int expireSeconds, final Type componentType) { + return (Collection) getexCollectionAsync(key, expireSeconds, componentType).join(); + } + + @Deprecated(since = "2.8.0") + default Collection getexStringCollection(String key, final int expireSeconds) { + return getexStringCollectionAsync(key, expireSeconds).join(); + } + + @Deprecated(since = "2.8.0") + default Collection getexLongCollection(String key, final int expireSeconds) { + return getexLongCollectionAsync(key, expireSeconds).join(); + } + + @Deprecated(since = "2.8.0") + default Map> getCollectionMap(final boolean set, final Type componentType, String... keys) { + return (Map) getCollectionMapAsync(set, componentType, keys).join(); + } }