优化CacheSource

This commit is contained in:
Redkale
2022-12-22 10:48:08 +08:00
parent 19b22161c9
commit 68ff0f7bab
2 changed files with 278 additions and 81 deletions

View File

@@ -923,6 +923,11 @@ public final class CacheMemorySource extends AbstractCacheSource {
return (Set<T>) get(key, componentType);
}
@Override
public <T> List<T> lrange(final String key, final Type componentType) {
return (List<T>) get(key, componentType);
}
@Override
public <T> Map<String, Set<T>> smembers(final Type componentType, final String... keys) {
Map<String, Set<T>> map = new HashMap<>();
@@ -933,6 +938,16 @@ public final class CacheMemorySource extends AbstractCacheSource {
return map;
}
@Override
public <T> Map<String, List<T>> lrange(final Type componentType, final String... keys) {
Map<String, List<T>> map = new HashMap<>();
for (String key : keys) {
List<T> s = (List<T>) get(key, componentType);
if (s != null) map.put(key, s);
}
return map;
}
@Override
public <T> Map<String, Collection<T>> getCollectionMap(final boolean set, final Type componentType, final String... keys) {
Map<String, Collection<T>> map = new HashMap<>();
@@ -1049,6 +1064,11 @@ public final class CacheMemorySource extends AbstractCacheSource {
return map;
}
@Override
public <T> CompletableFuture<Map<String, List<T>>> lrangeAsync(Type componentType, String... keys) {
return CompletableFuture.supplyAsync(() -> lrange(componentType, keys), getExecutor());
}
@Override
public <T> CompletableFuture<Map<String, Set<T>>> smembersAsync(Type componentType, String... keys) {
return CompletableFuture.supplyAsync(() -> smembers(componentType, keys), getExecutor());
@@ -1089,6 +1109,11 @@ public final class CacheMemorySource extends AbstractCacheSource {
return CompletableFuture.supplyAsync(() -> smembers(key, componentType), getExecutor());
}
@Override
public <T> CompletableFuture<List<T>> lrangeAsync(String key, Type componentType) {
return CompletableFuture.supplyAsync(() -> lrange(key, componentType), getExecutor());
}
@Override
public int getCollectionSize(final String key) {
Collection collection = (Collection) get(key, Object.class);
@@ -1122,25 +1147,25 @@ public final class CacheMemorySource extends AbstractCacheSource {
}
@Override
public boolean existsStringSetItem(final String key, final String value) {
public boolean sismemberString(final String key, final String value) {
Collection<String> list = getStringCollection(key);
return list != null && list.contains(value);
}
@Override
public CompletableFuture<Boolean> existsStringSetItemAsync(final String key, final String value) {
return CompletableFuture.supplyAsync(() -> existsStringSetItem(key, value), getExecutor());
public CompletableFuture<Boolean> sismemberStringAsync(final String key, final String value) {
return CompletableFuture.supplyAsync(() -> sismemberString(key, value), getExecutor());
}
@Override
public boolean existsLongSetItem(final String key, final long value) {
public boolean sismemberLong(final String key, final long value) {
Collection<Long> list = getLongCollection(key);
return list != null && list.contains(value);
}
@Override
public CompletableFuture<Boolean> existsLongSetItemAsync(final String key, final long value) {
return CompletableFuture.supplyAsync(() -> existsLongSetItem(key, value), getExecutor());
public CompletableFuture<Boolean> sismemberLongAsync(final String key, final long value) {
return CompletableFuture.supplyAsync(() -> sismemberLong(key, value), getExecutor());
}
@Override
@@ -1178,37 +1203,37 @@ public final class CacheMemorySource extends AbstractCacheSource {
}
@Override
public <T> void appendListItem(String key, Type componentType, T value) {
public <T> void rpush(String key, Type componentType, T value) {
appendListItem(CacheEntryType.OBJECT_LIST, key, value);
}
@Override
public void appendStringListItem(String key, String value) {
public void rpushString(String key, String value) {
appendListItem(CacheEntryType.STRING_LIST, key, value);
}
@Override
public void appendLongListItem(String key, long value) {
public void rpushLong(String key, long value) {
appendListItem(CacheEntryType.LONG_LIST, key, value);
}
@Override
public <T> CompletableFuture<Void> appendListItemAsync(final String key, final Type componentType, final T value) {
return CompletableFuture.runAsync(() -> appendListItem(key, componentType, value), getExecutor()).whenComplete(futureCompleteConsumer);
public <T> CompletableFuture<Void> rpushAsync(final String key, final Type componentType, final T value) {
return CompletableFuture.runAsync(() -> rpush(key, componentType, value), getExecutor()).whenComplete(futureCompleteConsumer);
}
@Override
public CompletableFuture<Void> appendStringListItemAsync(final String key, final String value) {
return CompletableFuture.runAsync(() -> appendStringListItem(key, value), getExecutor()).whenComplete(futureCompleteConsumer);
public CompletableFuture<Void> rpushStringAsync(final String key, final String value) {
return CompletableFuture.runAsync(() -> rpushString(key, value), getExecutor()).whenComplete(futureCompleteConsumer);
}
@Override
public CompletableFuture<Void> appendLongListItemAsync(final String key, final long value) {
return CompletableFuture.runAsync(() -> appendLongListItem(key, value), getExecutor()).whenComplete(futureCompleteConsumer);
public CompletableFuture<Void> rpushLongAsync(final String key, final long value) {
return CompletableFuture.runAsync(() -> rpushLong(key, value), getExecutor()).whenComplete(futureCompleteConsumer);
}
@Override
public <T> int removeListItem(String key, final Type componentType, T value) {
public <T> int lrem(String key, final Type componentType, T value) {
if (key == null) return 0;
CacheEntry entry = container.get(key);
if (entry == null || entry.listValue == null) return 0;
@@ -1216,7 +1241,7 @@ public final class CacheMemorySource extends AbstractCacheSource {
}
@Override
public int removeStringListItem(String key, String value) {
public int lremString(String key, String value) {
if (key == null) return 0;
CacheEntry entry = container.get(key);
if (entry == null || entry.listValue == null) return 0;
@@ -1224,7 +1249,7 @@ public final class CacheMemorySource extends AbstractCacheSource {
}
@Override
public int removeLongListItem(String key, long value) {
public int lremLong(String key, long value) {
if (key == null) return 0;
CacheEntry entry = container.get(key);
if (entry == null || entry.listValue == null) return 0;
@@ -1232,37 +1257,37 @@ public final class CacheMemorySource extends AbstractCacheSource {
}
@Override
public <T> CompletableFuture<Integer> removeListItemAsync(final String key, final Type componentType, T value) {
return CompletableFuture.supplyAsync(() -> removeListItem(key, componentType, value), getExecutor()).whenComplete(futureCompleteConsumer);
public <T> CompletableFuture<Integer> lremAsync(final String key, final Type componentType, T value) {
return CompletableFuture.supplyAsync(() -> lrem(key, componentType, value), getExecutor()).whenComplete(futureCompleteConsumer);
}
@Override
public CompletableFuture<Integer> removeStringListItemAsync(final String key, final String value) {
return CompletableFuture.supplyAsync(() -> removeStringListItem(key, value), getExecutor()).whenComplete(futureCompleteConsumer);
public CompletableFuture<Integer> lremStringAsync(final String key, final String value) {
return CompletableFuture.supplyAsync(() -> lremString(key, value), getExecutor()).whenComplete(futureCompleteConsumer);
}
@Override
public CompletableFuture<Integer> removeLongListItemAsync(final String key, final long value) {
return CompletableFuture.supplyAsync(() -> removeLongListItem(key, value), getExecutor()).whenComplete(futureCompleteConsumer);
public CompletableFuture<Integer> lremLongAsync(final String key, final long value) {
return CompletableFuture.supplyAsync(() -> lremLong(key, value), getExecutor()).whenComplete(futureCompleteConsumer);
}
@Override
public String spopStringSetItem(final String key) {
public String spopString(final String key) {
return (String) spop(key, String.class);
}
@Override
public Set<String> spopStringSetItem(final String key, int count) {
public Set<String> spopString(final String key, int count) {
return spop(key, count, String.class);
}
@Override
public Long spopLongSetItem(final String key) {
public Long spopLong(final String key) {
return (Long) spop(key, long.class);
}
@Override
public Set<Long> spopLongSetItem(final String key, int count) {
public Set<Long> spopLong(final String key, int count) {
return spop(key, count, long.class);
}
@@ -1325,12 +1350,12 @@ public final class CacheMemorySource extends AbstractCacheSource {
}
@Override
public void appendStringSetItem(String key, String value) {
public void saddString(String key, String value) {
appendSetItem(CacheEntryType.OBJECT_SET, key, value);
}
@Override
public void appendLongSetItem(String key, long value) {
public void saddLong(String key, long value) {
appendSetItem(CacheEntryType.OBJECT_SET, key, value);
}
@@ -1340,13 +1365,13 @@ public final class CacheMemorySource extends AbstractCacheSource {
}
@Override
public CompletableFuture<Void> appendStringSetItemAsync(final String key, final String value) {
return CompletableFuture.runAsync(() -> appendStringSetItem(key, value), getExecutor()).whenComplete(futureCompleteConsumer);
public CompletableFuture<Void> saddStringAsync(final String key, final String value) {
return CompletableFuture.runAsync(() -> saddString(key, value), getExecutor()).whenComplete(futureCompleteConsumer);
}
@Override
public CompletableFuture<Void> appendLongSetItemAsync(final String key, final long value) {
return CompletableFuture.runAsync(() -> appendLongSetItem(key, value), getExecutor()).whenComplete(futureCompleteConsumer);
public CompletableFuture<Void> saddLongAsync(final String key, final long value) {
return CompletableFuture.runAsync(() -> saddLong(key, value), getExecutor()).whenComplete(futureCompleteConsumer);
}
@Override
@@ -1358,7 +1383,7 @@ public final class CacheMemorySource extends AbstractCacheSource {
}
@Override
public int removeStringSetItem(String key, String value) {
public int sremString(String key, String value) {
if (key == null) return 0;
CacheEntry entry = container.get(key);
if (entry == null || entry.csetValue == null) return 0;
@@ -1366,7 +1391,7 @@ public final class CacheMemorySource extends AbstractCacheSource {
}
@Override
public int removeLongSetItem(String key, long value) {
public int sremLong(String key, long value) {
if (key == null) return 0;
CacheEntry entry = container.get(key);
if (entry == null || entry.csetValue == null) return 0;
@@ -1379,13 +1404,13 @@ public final class CacheMemorySource extends AbstractCacheSource {
}
@Override
public CompletableFuture<Integer> removeStringSetItemAsync(final String key, final String value) {
return CompletableFuture.supplyAsync(() -> removeStringSetItem(key, value), getExecutor()).whenComplete(futureCompleteConsumer);
public CompletableFuture<Integer> sremStringAsync(final String key, final String value) {
return CompletableFuture.supplyAsync(() -> sremString(key, value), getExecutor()).whenComplete(futureCompleteConsumer);
}
@Override
public CompletableFuture<Integer> removeLongSetItemAsync(final String key, final long value) {
return CompletableFuture.supplyAsync(() -> removeLongSetItem(key, value), getExecutor()).whenComplete(futureCompleteConsumer);
public CompletableFuture<Integer> sremLongAsync(final String key, final long value) {
return CompletableFuture.supplyAsync(() -> sremLong(key, value), getExecutor()).whenComplete(futureCompleteConsumer);
}
@Override
@@ -1525,23 +1550,23 @@ public final class CacheMemorySource extends AbstractCacheSource {
}
@Override
public CompletableFuture<String> spopStringSetItemAsync(String key) {
return CompletableFuture.supplyAsync(() -> spopStringSetItem(key), getExecutor()).whenComplete(futureCompleteConsumer);
public CompletableFuture<String> spopStringAsync(String key) {
return CompletableFuture.supplyAsync(() -> spopString(key), getExecutor()).whenComplete(futureCompleteConsumer);
}
@Override
public CompletableFuture<Set<String>> spopStringSetItemAsync(String key, int count) {
return CompletableFuture.supplyAsync(() -> spopStringSetItem(key, count), getExecutor()).whenComplete(futureCompleteConsumer);
public CompletableFuture<Set<String>> spopStringAsync(String key, int count) {
return CompletableFuture.supplyAsync(() -> spopString(key, count), getExecutor()).whenComplete(futureCompleteConsumer);
}
@Override
public CompletableFuture<Long> spopLongSetItemAsync(String key) {
return CompletableFuture.supplyAsync(() -> spopLongSetItem(key), getExecutor()).whenComplete(futureCompleteConsumer);
public CompletableFuture<Long> spopLongAsync(String key) {
return CompletableFuture.supplyAsync(() -> spopLong(key), getExecutor()).whenComplete(futureCompleteConsumer);
}
@Override
public CompletableFuture<Set<Long>> spopLongSetItemAsync(String key, int count) {
return CompletableFuture.supplyAsync(() -> spopLongSetItem(key, count), getExecutor()).whenComplete(futureCompleteConsumer);
public CompletableFuture<Set<Long>> spopLongAsync(String key, int count) {
return CompletableFuture.supplyAsync(() -> spopLong(key, count), getExecutor()).whenComplete(futureCompleteConsumer);
}
public static enum CacheEntryType {

View File

@@ -175,17 +175,23 @@ public interface CacheSource extends Resourcable {
public <T> Map<String, T> hmap(final String key, final Type type, int offset, int limit, String pattern);
//------------------------ list ------------------------
public <T> void appendListItem(final String key, final Type componentType, final T value);
public <T> List<T> lrange(final String key, final Type componentType);
public <T> int removeListItem(final String key, final Type componentType, final T value);
public <T> Map<String, List<T>> lrange(final Type componentType, final String... keys);
public void appendStringListItem(final String key, final String value);
public <T> void rpush(final String key, final Type componentType, final T value);
public int removeStringListItem(final String key, final String value);
public <T> int lrem(final String key, final Type componentType, final T value);
public void appendLongListItem(final String key, final long value);
//---------- list-string ----------
public void rpushString(final String key, final String value);
public int removeLongListItem(final String key, final long value);
public int lremString(final String key, final String value);
//---------- list-long ----------
public void rpushLong(final String key, final long value);
public int lremLong(final String key, final long value);
//------------------------ set ------------------------
public <T> Set<T> smembers(final String key, final Type componentType);
@@ -203,26 +209,26 @@ public interface CacheSource extends Resourcable {
public <T> Set<T> spop(final String key, final int count, final Type componentType);
//---------- set-string ----------
public boolean existsStringSetItem(final String key, final String value);
public boolean sismemberString(final String key, final String value);
public void appendStringSetItem(final String key, final String value);
public void saddString(final String key, final String value);
public int removeStringSetItem(final String key, final String value);
public int sremString(final String key, final String value);
public String spopStringSetItem(final String key);
public String spopString(final String key);
public Set<String> spopStringSetItem(final String key, final int count);
public Set<String> spopString(final String key, final int count);
//---------- set-long ----------
public boolean existsLongSetItem(final String key, final long value);
public boolean sismemberLong(final String key, final long value);
public void appendLongSetItem(final String key, final long value);
public void saddLong(final String key, final long value);
public int removeLongSetItem(final String key, final long value);
public int sremLong(final String key, final long value);
public Long spopLongSetItem(final String key);
public Long spopLong(final String key);
public Set<Long> spopLongSetItem(final String key, final int count);
public Set<Long> spopLong(final String key, final int count);
//------------------------ collection ------------------------
@Deprecated
@@ -419,18 +425,24 @@ public interface CacheSource extends Resourcable {
public <T> CompletableFuture<Map<String, T>> hmapAsync(final String key, final Type type, int offset, int limit, String pattern);
//------------------------ listAsync ------------------------
public <T> CompletableFuture<Void> appendListItemAsync(final String key, final Type componentType, final T value);
//------------------------ listAsync ------------------------
public <T> CompletableFuture<List<T>> lrangeAsync(final String key, final Type componentType);
public <T> CompletableFuture<Integer> removeListItemAsync(final String key, final Type componentType, final T value);
public <T> CompletableFuture<Map<String, List<T>>> lrangeAsync(final Type componentType, final String... keys);
public CompletableFuture<Void> appendStringListItemAsync(final String key, final String value);
public <T> CompletableFuture<Void> rpushAsync(final String key, final Type componentType, final T value);
public CompletableFuture<Integer> removeStringListItemAsync(final String key, final String value);
public <T> CompletableFuture<Integer> lremAsync(final String key, final Type componentType, final T value);
public CompletableFuture<Void> appendLongListItemAsync(final String key, final long value);
//---------- list-string ----------
public CompletableFuture<Void> rpushStringAsync(final String key, final String value);
public CompletableFuture<Integer> removeLongListItemAsync(final String key, final long value);
public CompletableFuture<Integer> lremStringAsync(final String key, final String value);
//---------- list-long ----------
public CompletableFuture<Void> rpushLongAsync(final String key, final long value);
public CompletableFuture<Integer> lremLongAsync(final String key, final long value);
//------------------------ setAsync ------------------------
public <T> CompletableFuture<Set<T>> smembersAsync(final String key, final Type componentType);
@@ -448,26 +460,26 @@ public interface CacheSource extends Resourcable {
public <T> CompletableFuture<Set<T>> spopAsync(final String key, final int count, final Type componentType);
//---------- set-string ----------
public CompletableFuture<Boolean> existsStringSetItemAsync(final String key, final String value);
public CompletableFuture<Boolean> sismemberStringAsync(final String key, final String value);
public CompletableFuture<Void> appendStringSetItemAsync(final String key, final String value);
public CompletableFuture<Void> saddStringAsync(final String key, final String value);
public CompletableFuture<Integer> removeStringSetItemAsync(final String key, final String value);
public CompletableFuture<Integer> sremStringAsync(final String key, final String value);
public CompletableFuture<String> spopStringSetItemAsync(final String key);
public CompletableFuture<String> spopStringAsync(final String key);
public CompletableFuture<Set<String>> spopStringSetItemAsync(final String key, final int count);
public CompletableFuture<Set<String>> spopStringAsync(final String key, final int count);
//---------- set-long ----------
public CompletableFuture<Boolean> existsLongSetItemAsync(final String key, final long value);
public CompletableFuture<Boolean> sismemberLongAsync(final String key, final long value);
public CompletableFuture<Void> appendLongSetItemAsync(final String key, final long value);
public CompletableFuture<Void> saddLongAsync(final String key, final long value);
public CompletableFuture<Integer> removeLongSetItemAsync(final String key, final long value);
public CompletableFuture<Integer> sremLongAsync(final String key, final long value);
public CompletableFuture<Long> spopLongSetItemAsync(final String key);
public CompletableFuture<Long> spopLongAsync(final String key);
public CompletableFuture<Set<Long>> spopLongSetItemAsync(final String key, final int count);
public CompletableFuture<Set<Long>> spopLongAsync(final String key, final int count);
//------------------------ collectionAsync ------------------------
@Deprecated
@@ -704,4 +716,164 @@ public interface CacheSource extends Resourcable {
default <T> CompletableFuture<Set<T>> spopSetItemAsync(final String key, final int count, final Type componentType) {
return spopAsync(key, count, componentType);
}
@Deprecated
default boolean existsStringSetItem(final String key, final String value) {
return sismemberString(key, value);
}
@Deprecated
default void appendStringSetItem(final String key, final String value) {
saddString(key, value);
}
@Deprecated
default int removeStringSetItem(final String key, final String value) {
return sremString(key, value);
}
@Deprecated
default String spopStringSetItem(final String key) {
return spopString(key);
}
@Deprecated
default Set<String> spopStringSetItem(final String key, final int count) {
return spopString(key, count);
}
@Deprecated
default boolean existsLongSetItem(final String key, final long value) {
return sismemberLong(key, value);
}
@Deprecated
default void appendLongSetItem(final String key, final long value) {
saddLong(key, value);
}
@Deprecated
default int removeLongSetItem(final String key, final long value) {
return sremLong(key, value);
}
@Deprecated
default Long spopLongSetItem(final String key) {
return spopLong(key);
}
@Deprecated
default Set<Long> spopLongSetItem(final String key, final int count) {
return spopLong(key, count);
}
@Deprecated
default CompletableFuture<Boolean> existsStringSetItemAsync(final String key, final String value) {
return sismemberStringAsync(key, value);
}
@Deprecated
default CompletableFuture<Void> appendStringSetItemAsync(final String key, final String value) {
return saddStringAsync(key, value);
}
@Deprecated
default CompletableFuture<Integer> removeStringSetItemAsync(final String key, final String value) {
return sremStringAsync(key, value);
}
@Deprecated
default CompletableFuture<String> spopStringSetItemAsync(final String key) {
return spopStringAsync(key);
}
@Deprecated
default CompletableFuture<Set<String>> spopStringSetItemAsync(final String key, final int count) {
return spopStringAsync(key, count);
}
@Deprecated
default CompletableFuture<Boolean> existsLongSetItemAsync(final String key, final long value) {
return sismemberLongAsync(key, value);
}
@Deprecated
default CompletableFuture<Void> appendLongSetItemAsync(final String key, final long value) {
return saddLongAsync(key, value);
}
@Deprecated
default CompletableFuture<Integer> removeLongSetItemAsync(final String key, final long value) {
return sremLongAsync(key, value);
}
@Deprecated
default CompletableFuture<Long> spopLongSetItemAsync(final String key) {
return spopLongAsync(key);
}
@Deprecated
default CompletableFuture<Set<Long>> spopLongSetItemAsync(final String key, final int count) {
return spopLongAsync(key, count);
}
@Deprecated
default <T> CompletableFuture<Void> appendListItemAsync(final String key, final Type componentType, final T value) {
return rpushAsync(key, componentType, value);
}
@Deprecated
default <T> CompletableFuture<Integer> removeListItemAsync(final String key, final Type componentType, final T value) {
return lremAsync(key, componentType, value);
}
@Deprecated
default CompletableFuture<Void> appendStringListItemAsync(final String key, final String value) {
return rpushStringAsync(key, value);
}
@Deprecated
default CompletableFuture<Integer> removeStringListItemAsync(final String key, final String value) {
return lremStringAsync(key, value);
}
@Deprecated
default CompletableFuture<Void> appendLongListItemAsync(final String key, final long value) {
return rpushLongAsync(key, value);
}
@Deprecated
default CompletableFuture<Integer> removeLongListItemAsync(final String key, final long value) {
return lremLongAsync(key, value);
}
@Deprecated
default <T> void appendListItem(final String key, final Type componentType, final T value) {
rpush(key, componentType, value);
}
@Deprecated
default <T> int removeListItem(final String key, final Type componentType, final T value) {
return lrem(key, componentType, value);
}
@Deprecated
default void appendStringListItem(final String key, final String value) {
rpushString(key, value);
}
@Deprecated
default int removeStringListItem(final String key, final String value) {
return lremString(key, value);
}
@Deprecated
default void appendLongListItem(final String key, final long value) {
rpushLong(key, value);
}
@Deprecated
default int removeLongListItem(final String key, final long value) {
return lremLong(key, value);
}
}