CacheSource增加sunion方法

This commit is contained in:
redkale
2023-06-26 15:30:31 +08:00
parent 564aa972dc
commit acee7cdfb0
2 changed files with 62 additions and 0 deletions

View File

@@ -958,6 +958,40 @@ public final class CacheMemorySource extends AbstractCacheSource {
}, getExecutor());
}
@Override
public <T> CompletableFuture<Set<T>> sunionAsync(final String key, final Type componentType, final String... key2s) {
return supplyAsync(() -> {
Set<T> rs = new HashSet<>();
CacheEntry entry = container.get(key);
if (entry == null || entry.csetValue == null) {
return rs;
}
rs.addAll(entry.csetValue);
for (String k : key2s) {
CacheEntry en2 = container.get(k);
if (en2 != null && en2.csetValue != null) {
rs.addAll(en2.csetValue);
}
}
return rs;
}, getExecutor());
}
@Override
public CompletableFuture<Long> sunionstoreAsync(final String key, final String srcKey, final String... srcKey2s) {
return supplyAsync(() -> {
Set rs = sunion(srcKey, Object.class, srcKey2s);
if (container.containsKey(key)) {
Set set = container.get(srcKey).csetValue;
set.clear();
set.addAll(rs);
} else {
appendSetItem(CacheEntryType.SET_OBJECT, key, rs);
}
return (long) rs.size();
}, getExecutor());
}
@Override
public CompletableFuture<Long> sinterstoreAsync(final String key, final String srcKey, final String... srcKey2s) {
return supplyAsync(() -> {

View File

@@ -623,6 +623,22 @@ public interface CacheSource extends Resourcable {
return sinterstoreAsync(key, srcKey, srcKey2s).join();
}
default <T> Set<T> sunion(String key, Type componentType, String... key2s) {
return (Set) sunionAsync(key, componentType, key2s).join();
}
default Set<String> sunionString(String key, String... key2s) {
return sunion(key, String.class, key2s);
}
default Set<Long> sunionLong(String key, String... key2s) {
return sunion(key, Long.class, key2s);
}
default long sunionstore(String key, String srcKey, String... srcKey2s) {
return sunionstoreAsync(key, srcKey, srcKey2s).join();
}
default long scard(String key) {
return scardAsync(key).join();
}
@@ -1316,6 +1332,18 @@ public interface CacheSource extends Resourcable {
public CompletableFuture<Long> sinterstoreAsync(String key, String srcKey, String... srcKey2s);
public <T> CompletableFuture<Set<T>> sunionAsync(String key, Type componentType, String... key2s);
default CompletableFuture<Set<String>> sunionStringAsync(String key, String... key2s) {
return sunionAsync(key, String.class, key2s);
}
default CompletableFuture<Set<Long>> sunionLongAsync(String key, String... key2s) {
return sunionAsync(key, Long.class, key2s);
}
public CompletableFuture<Long> sunionstoreAsync(String key, String srcKey, String... srcKey2s);
public CompletableFuture<Long> scardAsync(String key);
public <T> CompletableFuture<Set<T>> smembersAsync(String key, Type componentType);