From 300441b9f76b13d459d3f338dd1ab0e2c87ae192 Mon Sep 17 00:00:00 2001 From: Redkale <8730487+redkale@users.noreply.github.com> Date: Thu, 3 Jan 2019 09:33:19 +0800 Subject: [PATCH] =?UTF-8?q?DataSource=E5=A2=9E=E5=8A=A0getCollectionMap?= =?UTF-8?q?=E7=B3=BB=E5=88=97=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/org/redkale/source/CacheMemorySource.java | 156 +++++++++++------- src/org/redkale/source/CacheSource.java | 12 ++ 2 files changed, 112 insertions(+), 56 deletions(-) diff --git a/src/org/redkale/source/CacheMemorySource.java b/src/org/redkale/source/CacheMemorySource.java index 3814f505a..d6feddcee 100644 --- a/src/org/redkale/source/CacheMemorySource.java +++ b/src/org/redkale/source/CacheMemorySource.java @@ -200,62 +200,61 @@ public class CacheMemorySource extends AbstractService impleme } } - /** - * public static void main(String[] args) throws Exception { - * AnyValue.DefaultAnyValue conf = new AnyValue.DefaultAnyValue(); - * conf.addValue("node", new AnyValue.DefaultAnyValue().addValue("addr", "127.0.0.1").addValue("port", "6379")); - * - * CacheMemorySource source = new CacheMemorySource(); - * source.defaultConvert = JsonFactory.root().getConvert(); - * source.initValueType(String.class); //value用String类型 - * source.initTransient(false); - * source.init(conf); - * - * System.out.println("------------------------------------"); - * source.remove("key1"); - * source.remove("key2"); - * source.remove("300"); - * source.set("key1", "value1"); - * source.setString("keystr1", "strvalue1"); - * source.setLong("keylong1", 333L); - * source.set("300", "4000"); - * source.getAndRefresh("key1", 3500); - * System.out.println("[有值] 300 GET : " + source.get("300")); - * System.out.println("[有值] key1 GET : " + source.get("key1")); - * System.out.println("[无值] key2 GET : " + source.get("key2")); - * System.out.println("[有值] keylong1 GET : " + source.getLong("keylong1", 0L)); - * System.out.println("[有值] key1 EXISTS : " + source.exists("key1")); - * System.out.println("[无值] key2 EXISTS : " + source.exists("key2")); - * - * source.remove("keys3"); - * source.appendListItem("keys3", "vals1"); - * source.appendListItem("keys3", "vals2"); - * System.out.println("-------- keys3 追加了两个值 --------"); - * System.out.println("[两值] keys3 VALUES : " + source.getCollection("keys3")); - * System.out.println("[有值] keys3 EXISTS : " + source.exists("keys3")); - * source.removeListItem("keys3", "vals1"); - * System.out.println("[一值] keys3 VALUES : " + source.getCollection("keys3")); - * source.getCollectionAndRefresh("keys3", 3000); - * - * source.remove("sets3"); - * source.appendSetItem("sets3", "setvals1"); - * source.appendSetItem("sets3", "setvals2"); - * source.appendSetItem("sets3", "setvals1"); - * System.out.println("[两值] sets3 VALUES : " + source.getCollection("sets3")); - * System.out.println("[有值] sets3 EXISTS : " + source.exists("sets3")); - * source.removeSetItem("sets3", "setvals1"); - * System.out.println("[一值] sets3 VALUES : " + source.getCollection("sets3")); - * System.out.println("sets3 大小 : " + source.getCollectionSize("sets3")); - * System.out.println("all keys: " + source.queryKeys()); - * System.out.println("newnum 值 : " + source.incr("newnum")); - * System.out.println("newnum 值 : " + source.decr("newnum")); - * System.out.println("------------------------------------"); - * source.destroy(null); - * source.init(null); - * System.out.println("all keys: " + source.queryKeys()); - * System.out.println("[有值] keylong1 GET : " + source.getLong("keylong1", 0L)); - * } - */ + public static void main(String[] args) throws Exception { + AnyValue.DefaultAnyValue conf = new AnyValue.DefaultAnyValue(); + conf.addValue("node", new AnyValue.DefaultAnyValue().addValue("addr", "127.0.0.1").addValue("port", "6379")); + + CacheMemorySource source = new CacheMemorySource(); + source.defaultConvert = JsonFactory.root().getConvert(); + source.initValueType(String.class); //value用String类型 + source.initTransient(false); + source.init(conf); + + System.out.println("------------------------------------"); + source.remove("key1"); + source.remove("key2"); + source.remove("300"); + source.set("key1", "value1"); + source.setString("keystr1", "strvalue1"); + source.setLong("keylong1", 333L); + source.set("300", "4000"); + source.getAndRefresh("key1", 3500); + System.out.println("[有值] 300 GET : " + source.get("300")); + System.out.println("[有值] key1 GET : " + source.get("key1")); + System.out.println("[无值] key2 GET : " + source.get("key2")); + System.out.println("[有值] keylong1 GET : " + source.getLong("keylong1", 0L)); + System.out.println("[有值] key1 EXISTS : " + source.exists("key1")); + System.out.println("[无值] key2 EXISTS : " + source.exists("key2")); + + source.remove("keys3"); + source.appendListItem("keys3", "vals1"); + source.appendListItem("keys3", "vals2"); + System.out.println("-------- keys3 追加了两个值 --------"); + System.out.println("[两值] keys3 VALUES : " + source.getCollection("keys3")); + System.out.println("[有值] keys3 EXISTS : " + source.exists("keys3")); + source.removeListItem("keys3", "vals1"); + System.out.println("[一值] keys3 VALUES : " + source.getCollection("keys3")); + source.getCollectionAndRefresh("keys3", 3000); + + source.remove("sets3"); + source.appendSetItem("sets3", "setvals1"); + source.appendSetItem("sets3", "setvals2"); + source.appendSetItem("sets3", "setvals1"); + System.out.println("[两值] sets3 VALUES : " + source.getCollection("sets3")); + System.out.println("[有值] sets3 EXISTS : " + source.exists("sets3")); + source.removeSetItem("sets3", "setvals1"); + System.out.println("[一值] sets3 VALUES : " + source.getCollection("sets3")); + System.out.println("sets3 大小 : " + source.getCollectionSize("sets3")); + System.out.println("all keys: " + source.queryKeys()); + System.out.println("newnum 值 : " + source.incr("newnum")); + System.out.println("newnum 值 : " + source.decr("newnum")); + System.out.println("------------------------------------"); + source.destroy(null); + source.init(null); + System.out.println("all keys: " + source.queryKeys()); + System.out.println("[有值] keylong1 GET : " + source.getLong("keylong1", 0L)); + } + @Override public void close() throws Exception { //给Application 关闭时调用 destroy(null); @@ -677,16 +676,46 @@ public class CacheMemorySource extends AbstractService impleme return (Collection) get(key); } + @Override + public Map> getCollectionMap(final Type componentType, final String... keys) { + Map> map = new HashMap<>(); + for (String key : keys) { + Collection s = (Collection) get(key); + if (s != null) map.put(key, s); + } + return map; + } + @Override public Collection getStringCollection(final String key) { return (Collection) get(key); } + @Override + public Map> getStringCollectionMap(final String... keys) { + Map> map = new HashMap<>(); + for (String key : keys) { + Collection s = (Collection) get(key); + if (s != null) map.put(key, s); + } + return map; + } + @Override public Collection getLongCollection(final String key) { return (Collection) get(key); } + @Override + public Map> getLongCollectionMap(final String... keys) { + Map> map = new HashMap<>(); + for (String key : keys) { + Collection s = (Collection) get(key); + if (s != null) map.put(key, s); + } + return map; + } + @Override public CompletableFuture> getCollectionAsync(final String key) { return CompletableFuture.supplyAsync(() -> getCollection(key), getExecutor()); @@ -697,16 +726,31 @@ public class CacheMemorySource extends AbstractService impleme return CompletableFuture.supplyAsync(() -> getCollection(key, componentType), getExecutor()); } + @Override + public CompletableFuture>> getCollectionMapAsync(final Type componentType, final String... keys) { + return CompletableFuture.supplyAsync(() -> getCollectionMap(componentType, keys), getExecutor()); + } + @Override public CompletableFuture> getStringCollectionAsync(final String key) { return CompletableFuture.supplyAsync(() -> getStringCollection(key), getExecutor()); } + @Override + public CompletableFuture>> getStringCollectionMapAsync(final String... keys) { + return CompletableFuture.supplyAsync(() -> getStringCollectionMap(keys), getExecutor()); + } + @Override public CompletableFuture> getLongCollectionAsync(final String key) { return CompletableFuture.supplyAsync(() -> getLongCollection(key), getExecutor()); } + @Override + public CompletableFuture>> getLongCollectionMapAsync(final String... keys) { + return CompletableFuture.supplyAsync(() -> getLongCollectionMap(keys), getExecutor()); + } + @Override public int getCollectionSize(final String key) { Collection collection = (Collection) get(key); diff --git a/src/org/redkale/source/CacheSource.java b/src/org/redkale/source/CacheSource.java index d868a8521..6d2a8eb23 100644 --- a/src/org/redkale/source/CacheSource.java +++ b/src/org/redkale/source/CacheSource.java @@ -92,6 +92,8 @@ public interface CacheSource { public Collection getCollection(final String key, final Type componentType); + public Map> getCollectionMap(final Type componentType, final String... keys); + public int getCollectionSize(final String key); public Collection getCollectionAndRefresh(final String key, final int expireSeconds); @@ -138,6 +140,8 @@ public interface CacheSource { public Collection getStringCollection(final String key); + public Map> getStringCollectionMap(final String... keys); + public Collection getStringCollectionAndRefresh(final String key, final int expireSeconds); public void appendStringListItem(final String key, final String value); @@ -160,6 +164,8 @@ public interface CacheSource { public Collection getLongCollection(final String key); + public Map> getLongCollectionMap(final String... keys); + public Collection getLongCollectionAndRefresh(final String key, final int expireSeconds); public void appendLongListItem(final String key, final long value); @@ -235,6 +241,8 @@ public interface CacheSource { public CompletableFuture> getCollectionAsync(final String key, final Type componentType); + public CompletableFuture>> getCollectionMapAsync(final Type componentType, final String... keys); + public CompletableFuture getCollectionSizeAsync(final String key); public CompletableFuture> getCollectionAndRefreshAsync(final String key, final int expireSeconds); @@ -281,6 +289,8 @@ public interface CacheSource { public CompletableFuture> getStringCollectionAsync(final String key); + public CompletableFuture>> getStringCollectionMapAsync(final String... keys); + public CompletableFuture> getStringCollectionAndRefreshAsync(final String key, final int expireSeconds); public CompletableFuture appendStringListItemAsync(final String key, final String value); @@ -303,6 +313,8 @@ public interface CacheSource { public CompletableFuture> getLongCollectionAsync(final String key); + public CompletableFuture>> getLongCollectionMapAsync(final String... keys); + public CompletableFuture> getLongCollectionAndRefreshAsync(final String key, final int expireSeconds); public CompletableFuture appendLongListItemAsync(final String key, final long value);