CacheSource增加srandmember方法
This commit is contained in:
@@ -851,6 +851,51 @@ public final class CacheMemorySource extends AbstractCacheSource {
|
||||
return incrbyAsync(key, -num);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> CompletableFuture<List<T>> srandmemberAsync(String key, Type componentType, int count) {
|
||||
return supplyAsync(() -> {
|
||||
List<T> list = new ArrayList<>();
|
||||
CacheEntry entry = container.get(key);
|
||||
if (entry == null || entry.csetValue == null) {
|
||||
return list;
|
||||
}
|
||||
List<T> vals = new ArrayList<>(entry.csetValue);
|
||||
if (count < 0) { //可以重复
|
||||
for (int i = 0; i < Math.abs(count); i++) {
|
||||
int index = ThreadLocalRandom.current().nextInt(vals.size());
|
||||
T val = vals.get(index);
|
||||
list.add(val);
|
||||
}
|
||||
} else { //不可以重复
|
||||
if (count >= vals.size()) {
|
||||
return vals;
|
||||
}
|
||||
return vals.subList(0, count);
|
||||
}
|
||||
return list;
|
||||
}, getExecutor());
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> CompletableFuture<Boolean> smoveAsync(String key, String key2, Type componentType, T member) {
|
||||
return supplyAsync(() -> {
|
||||
CacheEntry entry = container.get(key);
|
||||
if (entry == null || entry.csetValue == null) {
|
||||
return false;
|
||||
}
|
||||
boolean rs = entry.csetValue.remove(member);
|
||||
if (rs) {
|
||||
CacheEntry entry2 = container.get(key2);
|
||||
if (entry2 == null || entry2.csetValue == null) {
|
||||
appendSetItem(componentType == String.class ? CacheEntryType.SET_STRING : CacheEntryType.SET_OBJECT, key2, List.of(member));
|
||||
} else {
|
||||
entry2.csetValue.add(member);
|
||||
}
|
||||
}
|
||||
return rs;
|
||||
}, getExecutor());
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> CompletableFuture<Set<T>> sdiffAsync(final String key, final Type componentType, final String... key2s) {
|
||||
return supplyAsync(() -> {
|
||||
|
||||
@@ -555,6 +555,42 @@ public interface CacheSource extends Resourcable {
|
||||
sadd(key, Long.class, values);
|
||||
}
|
||||
|
||||
default <T> boolean smove(String key, String key2, Type componentType, T member) {
|
||||
return smoveAsync(key, key2, componentType, member).join();
|
||||
}
|
||||
|
||||
default boolean smoveString(String key, String key2, String member) {
|
||||
return smove(key, key2, String.class, member);
|
||||
}
|
||||
|
||||
default boolean smoveLong(String key, String key2, Long member) {
|
||||
return smove(key, key2, Long.class, member);
|
||||
}
|
||||
|
||||
default <T> List<T> srandmember(String key, Type componentType, int count) {
|
||||
return (List) srandmemberAsync(key, componentType, count).join();
|
||||
}
|
||||
|
||||
default List<String> srandmemberString(String key, int count) {
|
||||
return srandmember(key, String.class, count);
|
||||
}
|
||||
|
||||
default List<Long> srandmemberLong(String key, int count) {
|
||||
return srandmember(key, Long.class, count);
|
||||
}
|
||||
|
||||
default <T> T srandmember(String key, Type componentType) {
|
||||
return (T) srandmemberAsync(key, componentType).join();
|
||||
}
|
||||
|
||||
default CompletableFuture<String> srandmemberString(String key) {
|
||||
return srandmember(key, String.class);
|
||||
}
|
||||
|
||||
default Long srandmemberLong(String key) {
|
||||
return srandmember(key, Long.class);
|
||||
}
|
||||
|
||||
default <T> Set<T> sdiff(String key, Type componentType, String... key2s) {
|
||||
return (Set) sdiffAsync(key, componentType, key2s).join();
|
||||
}
|
||||
@@ -1234,6 +1270,38 @@ public interface CacheSource extends Resourcable {
|
||||
return sdiffAsync(key, Long.class, key2s);
|
||||
}
|
||||
|
||||
public <T> CompletableFuture<Boolean> smoveAsync(String key, String key2, Type componentType, T member);
|
||||
|
||||
default CompletableFuture<Boolean> smoveStringAsync(String key, String key2, String member) {
|
||||
return smoveAsync(key, key2, String.class, member);
|
||||
}
|
||||
|
||||
default CompletableFuture<Boolean> smoveLongAsync(String key, String key2, Long member) {
|
||||
return smoveAsync(key, key2, Long.class, member);
|
||||
}
|
||||
|
||||
public <T> CompletableFuture<List<T>> srandmemberAsync(String key, Type componentType, int count);
|
||||
|
||||
default CompletableFuture<List<String>> srandmemberStringAsync(String key, int count) {
|
||||
return srandmemberAsync(key, String.class, count);
|
||||
}
|
||||
|
||||
default CompletableFuture<List<Long>> srandmemberLongAsync(String key, int count) {
|
||||
return srandmemberAsync(key, Long.class, count);
|
||||
}
|
||||
|
||||
default <T> CompletableFuture<T> srandmemberAsync(String key, Type componentType) {
|
||||
return srandmemberAsync(key, componentType, 1).thenApply(list -> list != null && !list.isEmpty() ? (T) list.get(0) : null);
|
||||
}
|
||||
|
||||
default CompletableFuture<String> srandmemberStringAsync(String key) {
|
||||
return srandmemberAsync(key, String.class);
|
||||
}
|
||||
|
||||
default CompletableFuture<Long> srandmemberLongAsync(String key) {
|
||||
return srandmemberAsync(key, Long.class);
|
||||
}
|
||||
|
||||
public CompletableFuture<Long> sdiffstoreAsync(String key, String srcKey, String... srcKey2s);
|
||||
|
||||
public <T> CompletableFuture<Set<T>> sinterAsync(String key, Type componentType, String... key2s);
|
||||
|
||||
Reference in New Issue
Block a user