From ca9a13ea27230962a46120523d7410f7419c700f Mon Sep 17 00:00:00 2001 From: redkale Date: Wed, 31 May 2023 10:39:39 +0800 Subject: [PATCH] =?UTF-8?q?CacheSource=E5=A2=9E=E5=8A=A0hvals=E3=80=81hget?= =?UTF-8?q?all=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../redkale/cluster/CacheClusterAgent.java | 2 +- .../org/redkale/source/CacheMemorySource.java | 132 ++++++++++++++++-- .../java/org/redkale/source/CacheSource.java | 36 ++++- 3 files changed, 157 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/redkale/cluster/CacheClusterAgent.java b/src/main/java/org/redkale/cluster/CacheClusterAgent.java index 2d319c3be..d28f6f8e3 100644 --- a/src/main/java/org/redkale/cluster/CacheClusterAgent.java +++ b/src/main/java/org/redkale/cluster/CacheClusterAgent.java @@ -237,7 +237,7 @@ public class CacheClusterAgent extends ClusterAgent implements Resourcable { } private CompletableFuture> queryAddress(final String serviceName) { - final CompletableFuture> future = source.hmapAsync(serviceName, AddressEntry.class, 0, 10000); + final CompletableFuture> future = source.hscanAsync(serviceName, AddressEntry.class, 0, 10000); return future.thenApply(map -> { final Set set = new HashSet<>(); map.forEach((n, v) -> { diff --git a/src/main/java/org/redkale/source/CacheMemorySource.java b/src/main/java/org/redkale/source/CacheMemorySource.java index acaa5e0b4..9af63f2f4 100644 --- a/src/main/java/org/redkale/source/CacheMemorySource.java +++ b/src/main/java/org/redkale/source/CacheMemorySource.java @@ -363,12 +363,42 @@ public final class CacheMemorySource extends AbstractCacheSource { } @Override - public Map hmap(final String key, final Type type, int offset, int limit) { - return hmap(key, type, offset, limit, null); + public Map hgetall(final String key, final Type type) { + return hgetall(CacheEntryType.MAP, key); } @Override - public Map hmap(final String key, final Type type, int offset, int limit, String pattern) { + public Map hgetallString(final String key) { + return hgetall(CacheEntryType.MAP, key); + } + + @Override + public Map hgetallLong(final String key) { + return hgetall(CacheEntryType.MAP, key); + } + + @Override + public List hvals(final String key, final Type type) { + return hvals(CacheEntryType.MAP, key); + } + + @Override + public List hvalsString(final String key) { + return hvals(CacheEntryType.MAP, key); + } + + @Override + public List hvalsLong(final String key) { + return hvals(CacheEntryType.MAP, key); + } + + @Override + public Map hscan(final String key, final Type type, int offset, int limit) { + return hscan(key, type, offset, limit, null); + } + + @Override + public Map hscan(final String key, final Type type, int offset, int limit, String pattern) { if (key == null) { return new HashMap(); } @@ -591,13 +621,43 @@ public final class CacheMemorySource extends AbstractCacheSource { } @Override - public CompletableFuture> hmapAsync(final String key, final Type type, int offset, int limit) { - return CompletableFuture.supplyAsync(() -> hmap(key, type, offset, limit), getExecutor()); + public CompletableFuture> hgetallAsync(final String key, final Type type) { + return CompletableFuture.supplyAsync(() -> hgetall(key, type), getExecutor()); } @Override - public CompletableFuture> hmapAsync(final String key, final Type type, int offset, int limit, String pattern) { - return CompletableFuture.supplyAsync(() -> hmap(key, type, offset, limit, pattern), getExecutor()); + public CompletableFuture> hgetallStringAsync(final String key) { + return CompletableFuture.supplyAsync(() -> hgetallString(key), getExecutor()); + } + + @Override + public CompletableFuture> hgetallLongAsync(final String key) { + return CompletableFuture.supplyAsync(() -> hgetallLong(key), getExecutor()); + } + + @Override + public CompletableFuture> hvalsAsync(final String key, final Type type) { + return CompletableFuture.supplyAsync(() -> hvals(key, type), getExecutor()); + } + + @Override + public CompletableFuture> hvalsStringAsync(final String key) { + return CompletableFuture.supplyAsync(() -> hvalsString(key), getExecutor()); + } + + @Override + public CompletableFuture> hvalsLongAsync(final String key) { + return CompletableFuture.supplyAsync(() -> hvalsLong(key), getExecutor()); + } + + @Override + public CompletableFuture> hscanAsync(final String key, final Type type, int offset, int limit) { + return CompletableFuture.supplyAsync(() -> hscan(key, type, offset, limit), getExecutor()); + } + + @Override + public CompletableFuture> hscanAsync(final String key, final Type type, int offset, int limit, String pattern) { + return CompletableFuture.supplyAsync(() -> hscan(key, type, offset, limit, pattern), getExecutor()); } @Override @@ -765,6 +825,30 @@ public final class CacheMemorySource extends AbstractCacheSource { } } + protected Map hgetall(CacheEntryType cacheType, String key) { + if (key == null) { + return new LinkedHashMap(); + } + CacheEntry entry = container.get(key); + if (entry == null) { + return new LinkedHashMap(); + } else { + return new LinkedHashMap(entry.mapValue); + } + } + + protected List hvals(CacheEntryType cacheType, String key) { + if (key == null) { + return new ArrayList(); + } + CacheEntry entry = container.get(key); + if (entry == null) { + return new ArrayList(); + } else { + return new ArrayList(entry.mapValue.values()); + } + } + @Override public void mset(Object... keyVals) { if (keyVals.length % 2 != 0) { @@ -1909,63 +1993,77 @@ public final class CacheMemorySource extends AbstractCacheSource { return mapValue; } } - + + //-------------------------- 过期方法 ---------------------------------- + @Override + @Deprecated(since = "2.8.0") public Collection getexLongCollection(String key, int expireSeconds) { return (Collection) getex(key, expireSeconds, long.class); } @Override + @Deprecated(since = "2.8.0") public CompletableFuture> getexCollectionAsync(final String key, final int expireSeconds, final Type componentType) { return CompletableFuture.supplyAsync(() -> getexCollection(key, expireSeconds, componentType), getExecutor()); } @Override + @Deprecated(since = "2.8.0") public CompletableFuture> getexStringCollectionAsync(final String key, final int expireSeconds) { return CompletableFuture.supplyAsync(() -> getexStringCollection(key, expireSeconds), getExecutor()); } @Override + @Deprecated(since = "2.8.0") public CompletableFuture> getexLongCollectionAsync(final String key, final int expireSeconds) { return CompletableFuture.supplyAsync(() -> getexLongCollection(key, expireSeconds), getExecutor()); } @Override + @Deprecated(since = "2.8.0") public CompletableFuture>> getCollectionMapAsync(boolean set, Type componentType, String... keys) { return CompletableFuture.supplyAsync(() -> getCollectionMap(set, componentType, keys), getExecutor()); } @Override + @Deprecated(since = "2.8.0") public CompletableFuture> getStringCollectionAsync(final String key) { return CompletableFuture.supplyAsync(() -> getStringCollection(key), getExecutor()); } @Override + @Deprecated(since = "2.8.0") public CompletableFuture>> getStringCollectionMapAsync(final boolean set, final String... keys) { return CompletableFuture.supplyAsync(() -> getStringCollectionMap(set, keys), getExecutor()); } @Override + @Deprecated(since = "2.8.0") public CompletableFuture> getLongCollectionAsync(final String key) { return CompletableFuture.supplyAsync(() -> getLongCollection(key), getExecutor()); } @Override + @Deprecated(since = "2.8.0") public CompletableFuture>> getLongCollectionMapAsync(final boolean set, final String... keys) { return CompletableFuture.supplyAsync(() -> getLongCollectionMap(set, keys), getExecutor()); } @Override + @Deprecated(since = "2.8.0") public CompletableFuture> getCollectionAsync(String key, Type componentType) { return CompletableFuture.supplyAsync(() -> getCollection(key, componentType), getExecutor()); } @Override + @Deprecated(since = "2.8.0") public Collection getCollection(final String key, final Type componentType) { return (Collection) get(key, componentType); } @Override + @Deprecated(since = "2.8.0") public Map> getCollectionMap(final boolean set, final Type componentType, final String... keys) { Map> map = new HashMap<>(); for (String key : keys) { @@ -1978,11 +2076,13 @@ public final class CacheMemorySource extends AbstractCacheSource { } @Override + @Deprecated(since = "2.8.0") public Collection getStringCollection(final String key) { return (Collection) get(key, String.class); } @Override + @Deprecated(since = "2.8.0") public Map> getStringCollectionMap(final boolean set, final String... keys) { Map> map = new HashMap<>(); for (String key : keys) { @@ -1995,6 +2095,7 @@ public final class CacheMemorySource extends AbstractCacheSource { } @Override + @Deprecated(since = "2.8.0") public Map getLongMap(final String... keys) { Map map = new LinkedHashMap<>(); for (String key : keys) { @@ -2005,6 +2106,7 @@ public final class CacheMemorySource extends AbstractCacheSource { } @Override + @Deprecated(since = "2.8.0") public Long[] getLongArray(final String... keys) { Long[] rs = new Long[keys.length]; int index = -1; @@ -2016,16 +2118,19 @@ public final class CacheMemorySource extends AbstractCacheSource { } @Override + @Deprecated(since = "2.8.0") public CompletableFuture> getLongMapAsync(final String... keys) { return CompletableFuture.supplyAsync(() -> getLongMap(keys), getExecutor()); } @Override + @Deprecated(since = "2.8.0") public CompletableFuture getLongArrayAsync(final String... keys) { return CompletableFuture.supplyAsync(() -> getLongArray(keys), getExecutor()); } @Override + @Deprecated(since = "2.8.0") public Map getStringMap(final String... keys) { Map map = new LinkedHashMap<>(); for (String key : keys) { @@ -2036,6 +2141,7 @@ public final class CacheMemorySource extends AbstractCacheSource { } @Override + @Deprecated(since = "2.8.0") public String[] getStringArray(final String... keys) { String[] rs = new String[keys.length]; int index = -1; @@ -2047,16 +2153,19 @@ public final class CacheMemorySource extends AbstractCacheSource { } @Override + @Deprecated(since = "2.8.0") public CompletableFuture> getStringMapAsync(final String... keys) { return CompletableFuture.supplyAsync(() -> getStringMap(keys), getExecutor()); } @Override + @Deprecated(since = "2.8.0") public CompletableFuture getStringArrayAsync(final String... keys) { return CompletableFuture.supplyAsync(() -> getStringArray(keys), getExecutor()); } @Override + @Deprecated(since = "2.8.0") public Map getMap(final Type componentType, final String... keys) { Map map = new LinkedHashMap<>(); for (String key : keys) { @@ -2066,16 +2175,19 @@ public final class CacheMemorySource extends AbstractCacheSource { } @Override + @Deprecated(since = "2.8.0") public CompletableFuture> getMapAsync(final Type componentType, final String... keys) { return CompletableFuture.supplyAsync(() -> getMap(componentType, keys), getExecutor()); } @Override + @Deprecated(since = "2.8.0") public Collection getLongCollection(final String key) { return (Collection) get(key, long.class); } @Override + @Deprecated(since = "2.8.0") public Map> getLongCollectionMap(final boolean set, final String... keys) { Map> map = new HashMap<>(); for (String key : keys) { @@ -2088,22 +2200,26 @@ public final class CacheMemorySource extends AbstractCacheSource { } @Override + @Deprecated(since = "2.8.0") public int getCollectionSize(final String key) { Collection collection = (Collection) get(key, Object.class); return collection == null ? 0 : collection.size(); } @Override + @Deprecated(since = "2.8.0") public CompletableFuture getCollectionSizeAsync(final String key) { return CompletableFuture.supplyAsync(() -> getCollectionSize(key), getExecutor()); } @Override + @Deprecated(since = "2.8.0") public Collection getexCollection(final String key, final int expireSeconds, final Type componentType) { return (Collection) getex(key, expireSeconds, componentType); } @Override + @Deprecated(since = "2.8.0") public Collection getexStringCollection(final String key, final int expireSeconds) { return (Collection) getex(key, expireSeconds, String.class); } diff --git a/src/main/java/org/redkale/source/CacheSource.java b/src/main/java/org/redkale/source/CacheSource.java index d1841420e..418466b4f 100644 --- a/src/main/java/org/redkale/source/CacheSource.java +++ b/src/main/java/org/redkale/source/CacheSource.java @@ -207,6 +207,20 @@ public interface CacheSource extends Resourcable { public boolean hsetnxLong(final String key, final String field, final long value); + //------------------------ hgetall ------------------------ + public Map hgetall(final String key, final Type type); + + public Map hgetallString(final String key); + + public Map hgetallLong(final String key); + + //------------------------ hvals ------------------------ + public List hvals(final String key, final Type type); + + public List hvalsString(final String key); + + public List hvalsLong(final String key); + //------------------------ hxxx ------------------------ public int hdel(final String key, String... fields); @@ -233,9 +247,9 @@ public interface CacheSource extends Resourcable { public List hmget(final String key, final Type type, final String... fields); - public Map hmap(final String key, final Type type, int offset, int limit); + public Map hscan(final String key, final Type type, int offset, int limit); - public Map hmap(final String key, final Type type, int offset, int limit, String pattern); + public Map hscan(final String key, final Type type, int offset, int limit, String pattern); //------------------------ list ------------------------ public int llen(final String key); @@ -498,6 +512,20 @@ public interface CacheSource extends Resourcable { public CompletableFuture hsetnxLongAsync(final String key, final String field, final long value); + //------------------------ hgetallAsync ------------------------ + public CompletableFuture> hgetallAsync(final String key, final Type type); + + public CompletableFuture> hgetallStringAsync(final String key); + + public CompletableFuture> hgetallLongAsync(final String key); + + //------------------------ hvalsAsync ------------------------ + public CompletableFuture> hvalsAsync(final String key, final Type type); + + public CompletableFuture> hvalsStringAsync(final String key); + + public CompletableFuture> hvalsLongAsync(final String key); + //------------------------ hxxxAsync ------------------------ public CompletableFuture hdelAsync(final String key, String... fields); @@ -524,9 +552,9 @@ public interface CacheSource extends Resourcable { public CompletableFuture> hmgetAsync(final String key, final Type type, final String... fields); - public CompletableFuture> hmapAsync(final String key, final Type type, int offset, int limit); + public CompletableFuture> hscanAsync(final String key, final Type type, int offset, int limit); - public CompletableFuture> hmapAsync(final String key, final Type type, int offset, int limit, String pattern); + public CompletableFuture> hscanAsync(final String key, final Type type, int offset, int limit, String pattern); //------------------------ listAsync ------------------------ public CompletableFuture llenAsync(final String key);