加入spop系列方法

This commit is contained in:
Redkale
2020-07-29 20:08:47 +08:00
parent a9dce0efbf
commit b68808b325
2 changed files with 113 additions and 0 deletions

View File

@@ -1328,6 +1328,65 @@ public final class CacheMemorySource<V extends Object> 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<String> 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<Long> spopLongSetItem(final String key, int count) {
return (List) spopSetItem(key, count, long.class);
}
@Override
public <T> 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 <T> List<T> 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<T> 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<V extends Object> extends AbstractService i
public CompletableFuture<Integer> getKeySizeAsync() {
return CompletableFuture.completedFuture(container.size());
}
@Override
public <T> CompletableFuture<T> spopSetItemAsync(String key, Type componentType) {
return CompletableFuture.supplyAsync(() -> spopSetItem(key, componentType), getExecutor()).whenComplete(futureCompleteConsumer);
}
@Override
public <T> CompletableFuture<List<T>> spopSetItemAsync(String key, int count, Type componentType) {
return CompletableFuture.supplyAsync(() -> spopSetItem(key, count, componentType), getExecutor()).whenComplete(futureCompleteConsumer);
}
@Override
public CompletableFuture<String> spopStringSetItemAsync(String key) {
return CompletableFuture.supplyAsync(() -> spopStringSetItem(key), getExecutor()).whenComplete(futureCompleteConsumer);
}
@Override
public CompletableFuture<List<String>> spopStringSetItemAsync(String key, int count) {
return CompletableFuture.supplyAsync(() -> spopStringSetItem(key, count), getExecutor()).whenComplete(futureCompleteConsumer);
}
@Override
public CompletableFuture<Long> spopLongSetItemAsync(String key) {
return CompletableFuture.supplyAsync(() -> spopLongSetItem(key), getExecutor()).whenComplete(futureCompleteConsumer);
}
@Override
public CompletableFuture<List<Long>> spopLongSetItemAsync(String key, int count) {
return CompletableFuture.supplyAsync(() -> spopLongSetItem(key, count), getExecutor()).whenComplete(futureCompleteConsumer);
}
}

View File

@@ -174,6 +174,10 @@ public interface CacheSource<V extends Object> {
public <T> int removeSetItem(final String key, final Type componentType, final T value);
public <T> T spopSetItem(final String key, final Type componentType);
public <T> List<T> spopSetItem(final String key, final int count, final Type componentType);
public List<String> queryKeys();
public List<String> queryKeysStartsWith(String startsWith);
@@ -204,6 +208,10 @@ public interface CacheSource<V extends Object> {
public void appendStringListItem(final String key, final String value);
public String spopStringSetItem(final String key);
public List<String> 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<V extends Object> {
public void appendLongListItem(final String key, final long value);
public long spopLongSetItem(final String key);
public List<Long> 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<V extends Object> {
public CompletableFuture<Void> appendListItemAsync(final String key, final V value);
public <T> CompletableFuture<T> spopSetItemAsync(final String key, final Type componentType);
public <T> CompletableFuture<List<T>> spopSetItemAsync(final String key, final int count, final Type componentType);
public CompletableFuture<Integer> removeListItemAsync(final String key, final V value);
public CompletableFuture<Boolean> existsSetItemAsync(final String key, final V value);
@@ -411,6 +427,10 @@ public interface CacheSource<V extends Object> {
public CompletableFuture<Void> appendStringListItemAsync(final String key, final String value);
public CompletableFuture<String> spopStringSetItemAsync(final String key);
public CompletableFuture<List<String>> spopStringSetItemAsync(final String key, final int count);
public CompletableFuture<Integer> removeStringListItemAsync(final String key, final String value);
public CompletableFuture<Boolean> existsStringSetItemAsync(final String key, final String value);
@@ -439,6 +459,10 @@ public interface CacheSource<V extends Object> {
public CompletableFuture<Void> appendLongListItemAsync(final String key, final long value);
public CompletableFuture<Long> spopLongSetItemAsync(final String key);
public CompletableFuture<List<Long>> spopLongSetItemAsync(final String key, final int count);
public CompletableFuture<Integer> removeLongListItemAsync(final String key, final long value);
public CompletableFuture<Boolean> existsLongSetItemAsync(final String key, final long value);