This commit is contained in:
wentch
2015-12-23 15:51:08 +08:00
parent d9c85106cf
commit 33944bd01c
3 changed files with 15 additions and 13 deletions

View File

@@ -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);

View File

@@ -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

View File

@@ -34,7 +34,7 @@ public class CacheSourceService<K extends Serializable, V extends Object> implem
@Resource
private JsonConvert convert;
private boolean needStore = true;
private boolean needStore;
private Class keyType;
@@ -53,6 +53,7 @@ public class CacheSourceService<K extends Serializable, V extends Object> implem
protected final ConcurrentHashMap<K, CacheEntry<K, ?>> container = new ConcurrentHashMap<>();
public CacheSourceService() {
CacheEntry.initCreator();
}
public final CacheSourceService setStoreType(Class keyType, Class valueType) {
@@ -60,6 +61,7 @@ public class CacheSourceService<K extends Serializable, V extends Object> 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<K extends Serializable, V extends Object> 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<K extends Serializable, V extends Object> 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<K extends Serializable, V extends Object> implem
String line;
while ((line = reader.readLine()) != null) {
if (line.isEmpty()) continue;
CacheEntry<K, ?> entry = convert.convertFrom(line.contains(CacheEntry.JSON_SET_KEY) ? storeSetType : (line.contains(CacheEntry.JSON_LIST_KEY) ? storeListType : storeObjType), line);
CacheEntry<K, ?> 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<K extends Serializable, V extends Object> implem
File store = new File(home, "cache/" + name());
store.getParentFile().mkdirs();
PrintStream stream = new PrintStream(store, "UTF-8");
Collection<CacheEntry<K, ?>> values = (Collection<CacheEntry<K, ?>>) 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<CacheEntry<K, ?>> entrys = (Collection<CacheEntry<K, ?>>) 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<K extends Serializable, V extends Object> implem
public static final class CacheEntry<K extends Serializable, T> {
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<CacheEntry> {