This commit is contained in:
wentch
2015-12-23 15:12:06 +08:00
parent 59f1d51d99
commit 6e78946d37
4 changed files with 36 additions and 5 deletions

View File

@@ -21,6 +21,7 @@ import java.util.*;
import java.util.function.Consumer;
import java.util.logging.*;
import javax.annotation.*;
import javax.persistence.*;
import org.redkale.net.*;
import org.redkale.net.http.*;
import org.redkale.service.*;
@@ -209,6 +210,7 @@ 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);
application.cacheSources.add(source);
regFactory.register(resourceName, CacheSource.class, source);

View File

@@ -276,6 +276,28 @@ public abstract class Factory<R extends Reader, W extends Writer> {
if (type instanceof ParameterizedType) {
final ParameterizedType pts = (ParameterizedType) type;
clazz = (Class) (pts).getRawType();
} else if (type instanceof TypeVariable) { // e.g. <? extends E>
final TypeVariable tv = (TypeVariable) type;
Class cz = tv.getBounds().length == 0 ? Object.class : null;
for (Type f : tv.getBounds()) {
if (f instanceof Class) {
cz = (Class) f;
break;
}
}
clazz = cz;
if (cz == null) throw new ConvertException("not support the type (" + type + ")");
} else if (type instanceof WildcardType) { // e.g. <? extends Serializable>
final WildcardType wt = (WildcardType) type;
Class cz = null;
for (Type f : wt.getUpperBounds()) {
if (f instanceof Class) {
cz = (Class) f;
break;
}
}
clazz = cz;
if (cz == null) throw new ConvertException("not support the type (" + type + ")");
} else if (type instanceof Class) {
clazz = (Class) type;
} else {

View File

@@ -26,7 +26,7 @@ import org.redkale.util.*;
* @author zhangjx
*/
@AutoLoad(false)
public class CacheSourceService<K extends Serializable, V> implements CacheSource<K, V>, Service, AutoCloseable {
public class CacheSourceService<K extends Serializable, V extends Object> implements CacheSource<K, V>, Service, AutoCloseable {
@Resource(name = "APP_HOME")
private File home;
@@ -34,6 +34,8 @@ public class CacheSourceService<K extends Serializable, V> implements CacheSourc
@Resource
private JsonConvert convert;
private boolean needStore = true;
private Class keyType;
private Type objValueType;
@@ -53,7 +55,7 @@ public class CacheSourceService<K extends Serializable, V> implements CacheSourc
public CacheSourceService() {
}
public CacheSourceService setStoreType(Class keyType, Class valueType) {
public final CacheSourceService setStoreType(Class keyType, Class valueType) {
this.keyType = keyType;
this.objValueType = valueType;
this.setValueType = TypeToken.createParameterizedType(null, CopyOnWriteArraySet.class, valueType);
@@ -61,11 +63,16 @@ public class CacheSourceService<K extends Serializable, V> implements CacheSourc
return this;
}
public final void setNeedStore(boolean needStore) {
this.needStore = needStore;
}
@Override
public void init(AnyValue conf) {
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) {
@@ -111,7 +118,7 @@ public class CacheSourceService<K extends Serializable, V> implements CacheSourc
boolean datasync = false; //是否从远程同步过数据
//----------同步数据……-----------
// TODO
if (this.keyType == null) return;
if (!this.needStore) return;
try {
CacheEntry.initCreator();
File store = new File(home, "cache/" + name());
@@ -149,7 +156,7 @@ public class CacheSourceService<K extends Serializable, V> implements CacheSourc
@Override
public void destroy(AnyValue conf) {
if (scheduler != null) scheduler.shutdownNow();
if (this.keyType == null || Sncp.isRemote(this) || container.isEmpty()) return;
if (!this.needStore || Sncp.isRemote(this) || container.isEmpty()) return;
try {
CacheEntry.initCreator();
File store = new File(home, "cache/" + name());

View File

@@ -16,7 +16,7 @@ import java.util.*;
* @see http://www.redkale.org
* @author zhangjx
*/
public interface CacheSource<K extends Serializable, V> {
public interface CacheSource<K extends Serializable, V extends Object> {
default boolean isOpen() {
return true;