This commit is contained in:
@@ -210,8 +210,8 @@ public abstract class NodeServer {
|
|||||||
Type genericType = field.getGenericType();
|
Type genericType = field.getGenericType();
|
||||||
ParameterizedType pt = (genericType instanceof ParameterizedType) ? (ParameterizedType) genericType : null;
|
ParameterizedType pt = (genericType instanceof ParameterizedType) ? (ParameterizedType) genericType : null;
|
||||||
Type valType = pt == null ? null : pt.getActualTypeArguments()[1];
|
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);
|
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);
|
application.cacheSources.add(source);
|
||||||
regFactory.register(resourceName, CacheSource.class, source);
|
regFactory.register(resourceName, CacheSource.class, source);
|
||||||
field.set(src, source);
|
field.set(src, source);
|
||||||
|
|||||||
@@ -165,7 +165,7 @@ public class JsonWriter implements Writer {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void writeSmallString(String value) {
|
public void writeSmallString(String value) {
|
||||||
writeTo(false, value);
|
writeTo(true, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -200,22 +200,22 @@ public class JsonWriter implements Writer {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final void writeInt(int value) {
|
public final void writeInt(int value) {
|
||||||
writeSmallString(String.valueOf(value));
|
writeTo(false, String.valueOf(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final void writeLong(long value) {
|
public final void writeLong(long value) {
|
||||||
writeSmallString(String.valueOf(value));
|
writeTo(false, String.valueOf(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final void writeFloat(float value) {
|
public final void writeFloat(float value) {
|
||||||
writeSmallString(String.valueOf(value));
|
writeTo(false, String.valueOf(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final void writeDouble(double value) {
|
public final void writeDouble(double value) {
|
||||||
writeSmallString(String.valueOf(value));
|
writeTo(false, String.valueOf(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ public class CacheSourceService<K extends Serializable, V extends Object> implem
|
|||||||
@Resource
|
@Resource
|
||||||
private JsonConvert convert;
|
private JsonConvert convert;
|
||||||
|
|
||||||
private boolean needStore = true;
|
private boolean needStore;
|
||||||
|
|
||||||
private Class keyType;
|
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<>();
|
protected final ConcurrentHashMap<K, CacheEntry<K, ?>> container = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
public CacheSourceService() {
|
public CacheSourceService() {
|
||||||
|
CacheEntry.initCreator();
|
||||||
}
|
}
|
||||||
|
|
||||||
public final CacheSourceService setStoreType(Class keyType, Class valueType) {
|
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.objValueType = valueType;
|
||||||
this.setValueType = TypeToken.createParameterizedType(null, CopyOnWriteArraySet.class, valueType);
|
this.setValueType = TypeToken.createParameterizedType(null, CopyOnWriteArraySet.class, valueType);
|
||||||
this.listValueType = TypeToken.createParameterizedType(null, ConcurrentLinkedQueue.class, valueType);
|
this.listValueType = TypeToken.createParameterizedType(null, ConcurrentLinkedQueue.class, valueType);
|
||||||
|
this.setNeedStore(this.keyType != null && this.keyType != Serializable.class && this.objValueType != null);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,7 +74,6 @@ public class CacheSourceService<K extends Serializable, V extends Object> implem
|
|||||||
final CacheSourceService self = this;
|
final CacheSourceService self = this;
|
||||||
AnyValue prop = conf == null ? null : conf.getAnyValue("property");
|
AnyValue prop = conf == null ? null : conf.getAnyValue("property");
|
||||||
if (keyType == null && prop != null) {
|
if (keyType == null && prop != null) {
|
||||||
this.needStore = prop.getBoolValue("store-value", true);
|
|
||||||
String storeKeyStr = prop.getValue("key-type");
|
String storeKeyStr = prop.getValue("key-type");
|
||||||
String storeValueStr = prop.getValue("value-type");
|
String storeValueStr = prop.getValue("value-type");
|
||||||
if (storeKeyStr != null && storeValueStr != null) {
|
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);
|
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");
|
String expireHandlerClass = prop == null ? null : prop.getValue("expirehandler");
|
||||||
if (expireHandlerClass != null) {
|
if (expireHandlerClass != null) {
|
||||||
@@ -136,7 +138,7 @@ public class CacheSourceService<K extends Serializable, V extends Object> implem
|
|||||||
String line;
|
String line;
|
||||||
while ((line = reader.readLine()) != null) {
|
while ((line = reader.readLine()) != null) {
|
||||||
if (line.isEmpty()) continue;
|
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 (entry.isExpired()) continue;
|
||||||
if (datasync && container.containsKey(entry.key)) continue; //已经同步了
|
if (datasync && container.containsKey(entry.key)) continue; //已经同步了
|
||||||
container.put(entry.key, entry);
|
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());
|
File store = new File(home, "cache/" + name());
|
||||||
store.getParentFile().mkdirs();
|
store.getParentFile().mkdirs();
|
||||||
PrintStream stream = new PrintStream(store, "UTF-8");
|
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 storeObjType = TypeToken.createParameterizedType(null, CacheEntry.class, keyType, objValueType);
|
||||||
final Type storeSetType = TypeToken.createParameterizedType(null, CacheEntry.class, keyType, setValueType);
|
final Type storeSetType = TypeToken.createParameterizedType(null, CacheEntry.class, keyType, setValueType);
|
||||||
final Type storeListType = TypeToken.createParameterizedType(null, CacheEntry.class, keyType, listValueType);
|
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));
|
stream.println(convert.convertTo(entry.isSetCacheType() ? storeSetType : (entry.isListCacheType() ? storeListType : storeObjType), entry));
|
||||||
}
|
}
|
||||||
container.clear();
|
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 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> {
|
public static class CacheEntryCreator implements Creator<CacheEntry> {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user