From 33944bd01cdd80e04999366303dd208ba7fcab02 Mon Sep 17 00:00:00 2001 From: wentch <22250530@qq.com> Date: Wed, 23 Dec 2015 15:51:08 +0800 Subject: [PATCH] --- src/org/redkale/boot/NodeServer.java | 2 +- src/org/redkale/convert/json/JsonWriter.java | 10 +++++----- src/org/redkale/service/CacheSourceService.java | 16 +++++++++------- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/org/redkale/boot/NodeServer.java b/src/org/redkale/boot/NodeServer.java index 4b612bbde..e8cda38fa 100644 --- a/src/org/redkale/boot/NodeServer.java +++ b/src/org/redkale/boot/NodeServer.java @@ -210,8 +210,8 @@ public abstract class NodeServer { Type genericType = field.getGenericType(); ParameterizedType pt = (genericType instanceof ParameterizedType) ? (ParameterizedType) genericType : null; Type valType = pt == null ? null : pt.getActualTypeArguments()[1]; - source.setNeedStore(field.getAnnotation(Transient.class) == null); source.setStoreType(pt == null ? Serializable.class : (Class) pt.getActualTypeArguments()[0], valType instanceof Class ? (Class) valType : Object.class); + if (field.getAnnotation(Transient.class) != null) source.setNeedStore(false); //必须在setStoreType之后 application.cacheSources.add(source); regFactory.register(resourceName, CacheSource.class, source); field.set(src, source); diff --git a/src/org/redkale/convert/json/JsonWriter.java b/src/org/redkale/convert/json/JsonWriter.java index 377aab23f..f97b7b29b 100644 --- a/src/org/redkale/convert/json/JsonWriter.java +++ b/src/org/redkale/convert/json/JsonWriter.java @@ -165,7 +165,7 @@ public class JsonWriter implements Writer { @Override public void writeSmallString(String value) { - writeTo(false, value); + writeTo(true, value); } @Override @@ -200,22 +200,22 @@ public class JsonWriter implements Writer { @Override public final void writeInt(int value) { - writeSmallString(String.valueOf(value)); + writeTo(false, String.valueOf(value)); } @Override public final void writeLong(long value) { - writeSmallString(String.valueOf(value)); + writeTo(false, String.valueOf(value)); } @Override public final void writeFloat(float value) { - writeSmallString(String.valueOf(value)); + writeTo(false, String.valueOf(value)); } @Override public final void writeDouble(double value) { - writeSmallString(String.valueOf(value)); + writeTo(false, String.valueOf(value)); } @Override diff --git a/src/org/redkale/service/CacheSourceService.java b/src/org/redkale/service/CacheSourceService.java index 217243f3f..723b8e45c 100644 --- a/src/org/redkale/service/CacheSourceService.java +++ b/src/org/redkale/service/CacheSourceService.java @@ -34,7 +34,7 @@ public class CacheSourceService implem @Resource private JsonConvert convert; - private boolean needStore = true; + private boolean needStore; private Class keyType; @@ -53,6 +53,7 @@ public class CacheSourceService implem protected final ConcurrentHashMap> container = new ConcurrentHashMap<>(); public CacheSourceService() { + CacheEntry.initCreator(); } public final CacheSourceService setStoreType(Class keyType, Class valueType) { @@ -60,6 +61,7 @@ public class CacheSourceService implem this.objValueType = valueType; this.setValueType = TypeToken.createParameterizedType(null, CopyOnWriteArraySet.class, valueType); this.listValueType = TypeToken.createParameterizedType(null, ConcurrentLinkedQueue.class, valueType); + this.setNeedStore(this.keyType != null && this.keyType != Serializable.class && this.objValueType != null); return this; } @@ -72,7 +74,6 @@ public class CacheSourceService implem final CacheSourceService self = this; AnyValue prop = conf == null ? null : conf.getAnyValue("property"); if (keyType == null && prop != null) { - this.needStore = prop.getBoolValue("store-value", true); String storeKeyStr = prop.getValue("key-type"); String storeValueStr = prop.getValue("value-type"); if (storeKeyStr != null && storeValueStr != null) { @@ -82,6 +83,7 @@ public class CacheSourceService implem logger.log(Level.SEVERE, self.getClass().getSimpleName() + " load key & value store class (" + storeKeyStr + ", " + storeValueStr + ") error", e); } } + if (prop.getBoolValue("store-ignore", false)) setNeedStore(false); } String expireHandlerClass = prop == null ? null : prop.getValue("expirehandler"); if (expireHandlerClass != null) { @@ -136,7 +138,7 @@ public class CacheSourceService implem String line; while ((line = reader.readLine()) != null) { if (line.isEmpty()) continue; - CacheEntry entry = convert.convertFrom(line.contains(CacheEntry.JSON_SET_KEY) ? storeSetType : (line.contains(CacheEntry.JSON_LIST_KEY) ? storeListType : storeObjType), line); + CacheEntry entry = convert.convertFrom(line.startsWith(CacheEntry.JSON_SET_KEY) ? storeSetType : (line.startsWith(CacheEntry.JSON_LIST_KEY) ? storeListType : storeObjType), line); if (entry.isExpired()) continue; if (datasync && container.containsKey(entry.key)) continue; //已经同步了 container.put(entry.key, entry); @@ -162,11 +164,11 @@ public class CacheSourceService implem File store = new File(home, "cache/" + name()); store.getParentFile().mkdirs(); PrintStream stream = new PrintStream(store, "UTF-8"); - Collection> values = (Collection>) container.values(); final Type storeObjType = TypeToken.createParameterizedType(null, CacheEntry.class, keyType, objValueType); final Type storeSetType = TypeToken.createParameterizedType(null, CacheEntry.class, keyType, setValueType); final Type storeListType = TypeToken.createParameterizedType(null, CacheEntry.class, keyType, listValueType); - for (CacheEntry entry : values) { + Collection> entrys = (Collection>) container.values(); + for (CacheEntry entry : entrys) { stream.println(convert.convertTo(entry.isSetCacheType() ? storeSetType : (entry.isListCacheType() ? storeListType : storeObjType), entry)); } container.clear(); @@ -407,9 +409,9 @@ public class CacheSourceService implem public static final class CacheEntry { - public static final String JSON_SET_KEY = "\"cacheType\":\"" + CacheEntryType.SET + "\""; + public static final String JSON_SET_KEY = "{\"cacheType\":\"" + CacheEntryType.SET + "\""; - public static final String JSON_LIST_KEY = "\"cacheType\":\"" + CacheEntryType.LIST + "\""; + public static final String JSON_LIST_KEY = "{\"cacheType\":\"" + CacheEntryType.LIST + "\""; public static class CacheEntryCreator implements Creator {