diff --git a/src/org/redkale/source/CacheMemorySource.java b/src/org/redkale/source/CacheMemorySource.java index f702c435c..4daa4819a 100644 --- a/src/org/redkale/source/CacheMemorySource.java +++ b/src/org/redkale/source/CacheMemorySource.java @@ -1328,6 +1328,65 @@ public final class CacheMemorySource extends AbstractService i return CompletableFuture.supplyAsync(() -> removeLongListItem(key, value), getExecutor()).whenComplete(futureCompleteConsumer); } + @Override + public String spopStringSetItem(final String key) { + return (String) spopSetItem(key, String.class); + } + + @Override + public List spopStringSetItem(final String key, int count) { + return (List) spopSetItem(key, count, String.class); + } + + @Override + public long spopLongSetItem(final String key) { + return (Long) spopSetItem(key, long.class); + } + + @Override + public List spopLongSetItem(final String key, int count) { + return (List) spopSetItem(key, count, long.class); + } + + @Override + public T spopSetItem(final String key, final Type componentType) { + if (key == null) return null; + CacheEntry entry = container.get(key); + if (entry == null || !entry.isSetCacheType() || entry.csetValue == null) { + return null; + } + if (entry.csetValue.isEmpty()) return null; + Iterator it = entry.csetValue.iterator(); + if (it.hasNext()) { + Object obj = it.next(); + if (obj != null && componentType == long.class) obj = ((Number) obj).longValue(); + it.remove(); + return (T) obj; + } + return null; + } + + @Override + public List spopSetItem(final String key, final int count, final Type componentType) { + if (key == null) return new ArrayList<>(); + CacheEntry entry = container.get(key); + if (entry == null || !entry.isSetCacheType() || entry.csetValue == null) { + return new ArrayList<>(); + } + if (entry.csetValue.isEmpty()) return new ArrayList<>(); + Iterator it = entry.csetValue.iterator(); + List list = new ArrayList<>(); + int index = 0; + while (it.hasNext()) { + Object obj = it.next(); + if (obj != null && componentType == long.class) obj = ((Number) obj).longValue(); + list.add((T) obj); + it.remove(); + if (++index >= count) break; + } + return list; + } + protected void appendSetItem(CacheEntryType cacheType, String key, Object value) { if (key == null) return; CacheEntry entry = container.get(key); @@ -1489,4 +1548,34 @@ public final class CacheMemorySource extends AbstractService i public CompletableFuture getKeySizeAsync() { return CompletableFuture.completedFuture(container.size()); } + + @Override + public CompletableFuture spopSetItemAsync(String key, Type componentType) { + return CompletableFuture.supplyAsync(() -> spopSetItem(key, componentType), getExecutor()).whenComplete(futureCompleteConsumer); + } + + @Override + public CompletableFuture> spopSetItemAsync(String key, int count, Type componentType) { + return CompletableFuture.supplyAsync(() -> spopSetItem(key, count, componentType), getExecutor()).whenComplete(futureCompleteConsumer); + } + + @Override + public CompletableFuture spopStringSetItemAsync(String key) { + return CompletableFuture.supplyAsync(() -> spopStringSetItem(key), getExecutor()).whenComplete(futureCompleteConsumer); + } + + @Override + public CompletableFuture> spopStringSetItemAsync(String key, int count) { + return CompletableFuture.supplyAsync(() -> spopStringSetItem(key, count), getExecutor()).whenComplete(futureCompleteConsumer); + } + + @Override + public CompletableFuture spopLongSetItemAsync(String key) { + return CompletableFuture.supplyAsync(() -> spopLongSetItem(key), getExecutor()).whenComplete(futureCompleteConsumer); + } + + @Override + public CompletableFuture> spopLongSetItemAsync(String key, int count) { + return CompletableFuture.supplyAsync(() -> spopLongSetItem(key, count), getExecutor()).whenComplete(futureCompleteConsumer); + } } diff --git a/src/org/redkale/source/CacheSource.java b/src/org/redkale/source/CacheSource.java index e5bb044dc..6551b08ec 100644 --- a/src/org/redkale/source/CacheSource.java +++ b/src/org/redkale/source/CacheSource.java @@ -174,6 +174,10 @@ public interface CacheSource { public int removeSetItem(final String key, final Type componentType, final T value); + public T spopSetItem(final String key, final Type componentType); + + public List spopSetItem(final String key, final int count, final Type componentType); + public List queryKeys(); public List queryKeysStartsWith(String startsWith); @@ -204,6 +208,10 @@ public interface CacheSource { public void appendStringListItem(final String key, final String value); + public String spopStringSetItem(final String key); + + public List spopStringSetItem(final String key, final int count); + public int removeStringListItem(final String key, final String value); public boolean existsStringSetItem(final String key, final String value); @@ -232,6 +240,10 @@ public interface CacheSource { public void appendLongListItem(final String key, final long value); + public long spopLongSetItem(final String key); + + public List spopLongSetItem(final String key, final int count); + public int removeLongListItem(final String key, final long value); public boolean existsLongSetItem(final String key, final long value); @@ -363,6 +375,10 @@ public interface CacheSource { public CompletableFuture appendListItemAsync(final String key, final V value); + public CompletableFuture spopSetItemAsync(final String key, final Type componentType); + + public CompletableFuture> spopSetItemAsync(final String key, final int count, final Type componentType); + public CompletableFuture removeListItemAsync(final String key, final V value); public CompletableFuture existsSetItemAsync(final String key, final V value); @@ -411,6 +427,10 @@ public interface CacheSource { public CompletableFuture appendStringListItemAsync(final String key, final String value); + public CompletableFuture spopStringSetItemAsync(final String key); + + public CompletableFuture> spopStringSetItemAsync(final String key, final int count); + public CompletableFuture removeStringListItemAsync(final String key, final String value); public CompletableFuture existsStringSetItemAsync(final String key, final String value); @@ -439,6 +459,10 @@ public interface CacheSource { public CompletableFuture appendLongListItemAsync(final String key, final long value); + public CompletableFuture spopLongSetItemAsync(final String key); + + public CompletableFuture> spopLongSetItemAsync(final String key, final int count); + public CompletableFuture removeLongListItemAsync(final String key, final long value); public CompletableFuture existsLongSetItemAsync(final String key, final long value);