DataSource增加getCollectionMap系列接口

This commit is contained in:
Redkale
2019-01-03 09:33:19 +08:00
parent 1a5e9022ae
commit 300441b9f7
2 changed files with 112 additions and 56 deletions

View File

@@ -200,62 +200,61 @@ public class CacheMemorySource<V extends Object> 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<V extends Object> extends AbstractService impleme
return (Collection<T>) get(key);
}
@Override
public <T> Map<String, Collection<T>> getCollectionMap(final Type componentType, final String... keys) {
Map<String, Collection<T>> map = new HashMap<>();
for (String key : keys) {
Collection<T> s = (Collection<T>) get(key);
if (s != null) map.put(key, s);
}
return map;
}
@Override
public Collection<String> getStringCollection(final String key) {
return (Collection<String>) get(key);
}
@Override
public Map<String, Collection<String>> getStringCollectionMap(final String... keys) {
Map<String, Collection<String>> map = new HashMap<>();
for (String key : keys) {
Collection<String> s = (Collection<String>) get(key);
if (s != null) map.put(key, s);
}
return map;
}
@Override
public Collection<Long> getLongCollection(final String key) {
return (Collection<Long>) get(key);
}
@Override
public Map<String, Collection<Long>> getLongCollectionMap(final String... keys) {
Map<String, Collection<Long>> map = new HashMap<>();
for (String key : keys) {
Collection<Long> s = (Collection<Long>) get(key);
if (s != null) map.put(key, s);
}
return map;
}
@Override
public CompletableFuture<Collection<V>> getCollectionAsync(final String key) {
return CompletableFuture.supplyAsync(() -> getCollection(key), getExecutor());
@@ -697,16 +726,31 @@ public class CacheMemorySource<V extends Object> extends AbstractService impleme
return CompletableFuture.supplyAsync(() -> getCollection(key, componentType), getExecutor());
}
@Override
public CompletableFuture<Map<String, Collection<V>>> getCollectionMapAsync(final Type componentType, final String... keys) {
return CompletableFuture.supplyAsync(() -> getCollectionMap(componentType, keys), getExecutor());
}
@Override
public CompletableFuture<Collection<String>> getStringCollectionAsync(final String key) {
return CompletableFuture.supplyAsync(() -> getStringCollection(key), getExecutor());
}
@Override
public CompletableFuture<Map<String, Collection<String>>> getStringCollectionMapAsync(final String... keys) {
return CompletableFuture.supplyAsync(() -> getStringCollectionMap(keys), getExecutor());
}
@Override
public CompletableFuture<Collection<Long>> getLongCollectionAsync(final String key) {
return CompletableFuture.supplyAsync(() -> getLongCollection(key), getExecutor());
}
@Override
public CompletableFuture<Map<String, Collection<Long>>> getLongCollectionMapAsync(final String... keys) {
return CompletableFuture.supplyAsync(() -> getLongCollectionMap(keys), getExecutor());
}
@Override
public int getCollectionSize(final String key) {
Collection<V> collection = (Collection<V>) get(key);

View File

@@ -92,6 +92,8 @@ public interface CacheSource<V extends Object> {
public <T> Collection<T> getCollection(final String key, final Type componentType);
public <T> Map<String, Collection<T>> getCollectionMap(final Type componentType, final String... keys);
public int getCollectionSize(final String key);
public Collection<V> getCollectionAndRefresh(final String key, final int expireSeconds);
@@ -138,6 +140,8 @@ public interface CacheSource<V extends Object> {
public Collection<String> getStringCollection(final String key);
public Map<String, Collection<String>> getStringCollectionMap(final String... keys);
public Collection<String> getStringCollectionAndRefresh(final String key, final int expireSeconds);
public void appendStringListItem(final String key, final String value);
@@ -160,6 +164,8 @@ public interface CacheSource<V extends Object> {
public Collection<Long> getLongCollection(final String key);
public Map<String, Collection<Long>> getLongCollectionMap(final String... keys);
public Collection<Long> getLongCollectionAndRefresh(final String key, final int expireSeconds);
public void appendLongListItem(final String key, final long value);
@@ -235,6 +241,8 @@ public interface CacheSource<V extends Object> {
public <T> CompletableFuture<Collection<T>> getCollectionAsync(final String key, final Type componentType);
public <T> CompletableFuture<Map<String, Collection<T>>> getCollectionMapAsync(final Type componentType, final String... keys);
public CompletableFuture<Integer> getCollectionSizeAsync(final String key);
public CompletableFuture<Collection<V>> getCollectionAndRefreshAsync(final String key, final int expireSeconds);
@@ -281,6 +289,8 @@ public interface CacheSource<V extends Object> {
public CompletableFuture<Collection<String>> getStringCollectionAsync(final String key);
public CompletableFuture<Map<String, Collection<String>>> getStringCollectionMapAsync(final String... keys);
public CompletableFuture<Collection<String>> getStringCollectionAndRefreshAsync(final String key, final int expireSeconds);
public CompletableFuture<Void> appendStringListItemAsync(final String key, final String value);
@@ -303,6 +313,8 @@ public interface CacheSource<V extends Object> {
public CompletableFuture<Collection<Long>> getLongCollectionAsync(final String key);
public CompletableFuture<Map<String, Collection<Long>>> getLongCollectionMapAsync(final String... keys);
public CompletableFuture<Collection<Long>> getLongCollectionAndRefreshAsync(final String key, final int expireSeconds);
public CompletableFuture<Void> appendLongListItemAsync(final String key, final long value);