Commit 3143f352 authored by shenjiaqing's avatar shenjiaqing

提交代码

parent e7219e3d
...@@ -10,6 +10,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; ...@@ -10,6 +10,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.MapType; import com.fasterxml.jackson.databind.type.MapType;
import com.fasterxml.jackson.databind.type.TypeFactory; import com.fasterxml.jackson.databind.type.TypeFactory;
import com.github.benmanes.caffeine.cache.AsyncCache; import com.github.benmanes.caffeine.cache.AsyncCache;
import com.google.common.cache.Cache;
import com.netflix.appinfo.InstanceInfo; import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient; import com.netflix.discovery.EurekaClient;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
...@@ -80,6 +81,7 @@ public class CacheConfig { ...@@ -80,6 +81,7 @@ public class CacheConfig {
} }
for (Field field : fields) { for (Field field : fields) {
boolean isGuava = Cache.class.isAssignableFrom(field.getType());
boolean isCaffeine = AsyncCache.class.isAssignableFrom(field.getType()); boolean isCaffeine = AsyncCache.class.isAssignableFrom(field.getType());
if (!isCaffeine) { if (!isCaffeine) {
...@@ -89,13 +91,17 @@ public class CacheConfig { ...@@ -89,13 +91,17 @@ public class CacheConfig {
String keyName = String.format("%s-%s", beanName, field.getName()); String keyName = String.format("%s-%s", beanName, field.getName());
try { try {
Object cache = field.get(bean); Object cache = field.get(bean);
if (cache instanceof Cache) {
if (!(cache instanceof AsyncCache)) { ConcurrentMap<Object, Object> concurrentMap = ((Cache) cache).asMap();
continue; CacheManager.saveCache(keyName, concurrentMap);
logger.info("beanName = {}, cache = {}, isGuava = {}, bean = {}", keyName, cache, isGuava, bean);
} }
if (cache instanceof AsyncCache) {
ConcurrentMap<Object, Object> concurrentMap = ((AsyncCache) cache).synchronous().asMap(); ConcurrentMap<Object, Object> concurrentMap = ((AsyncCache) cache).synchronous().asMap();
CacheManager.saveCache(keyName, concurrentMap); CacheManager.saveCache(keyName, concurrentMap);
logger.info("beanName = {}, cache = {}, isCaffeine = {}, bean = {}", keyName, cache, isCaffeine, bean); logger.info("beanName = {}, cache = {}, isCaffeine = {}, bean = {}", keyName, cache, isCaffeine, bean);
}
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
//ignore //ignore
} finally { } finally {
...@@ -146,12 +152,10 @@ public class CacheConfig { ...@@ -146,12 +152,10 @@ public class CacheConfig {
field.setAccessible(true); field.setAccessible(true);
Object obj = field.get(bean); Object obj = field.get(bean);
if (!(obj instanceof AsyncCache)) { if (!(obj instanceof Cache) || !(obj instanceof AsyncCache)) {
continue; continue;
} }
AsyncCache asyncCache = (AsyncCache) obj;
// 通过MapType解析map结构 // 通过MapType解析map结构
ParameterizedType genericType = (ParameterizedType) field.getGenericType(); ParameterizedType genericType = (ParameterizedType) field.getGenericType();
Type[] actualTypeArguments = genericType.getActualTypeArguments(); Type[] actualTypeArguments = genericType.getActualTypeArguments();
...@@ -159,15 +163,33 @@ public class CacheConfig { ...@@ -159,15 +163,33 @@ public class CacheConfig {
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Map convertMap = objectMapper.convertValue(v, mapType); Map convertMap = objectMapper.convertValue(v, mapType);
if (obj instanceof Cache) {
Cache cache = (Cache) obj;
convertMap.forEach((k1, v1) -> { convertMap.forEach((k1, v1) -> {
// 通过JavaType解析map中的value结构 // 通过JavaType解析map中的value结构
ParameterizedType genericType1 = (ParameterizedType) actualTypeArguments[1]; ParameterizedType genericType1 = (ParameterizedType) actualTypeArguments[1];
Type[] actualTypeArguments1 = genericType1.getActualTypeArguments(); Type[] actualTypeArguments1 = genericType1.getActualTypeArguments();
JavaType javaType = typeFactory.constructType(actualTypeArguments1[0]); JavaType javaType = typeFactory.constructType(actualTypeArguments1[0]);
asyncCache.put(k1, CompletableFuture.supplyAsync(() -> Optional.ofNullable(objectMapper.convertValue(v1, javaType)))); cache.put(k1, Optional.ofNullable(objectMapper.convertValue(v1, javaType)));
}); });
logger.info("Guava Cache key = {} ; Guava Cache Size = {}", k, cache.asMap().size());
}
if (obj instanceof AsyncCache) {
AsyncCache asyncCache = (AsyncCache) obj;
convertMap.forEach((k1, v1) -> {
// 通过JavaType解析map中的value结构
ParameterizedType genericType1 = (ParameterizedType) actualTypeArguments[1];
Type[] actualTypeArguments1 = genericType1.getActualTypeArguments();
JavaType javaType = typeFactory.constructType(actualTypeArguments1[0]);
asyncCache.put(k1, CompletableFuture.supplyAsync(() -> Optional.ofNullable(objectMapper.convertValue(v1, javaType))));
});
logger.info("Caffeine Cache key = {} ; Caffeine Cache Size = {}", k, asyncCache.synchronous().asMap().size()); logger.info("Caffeine Cache key = {} ; Caffeine Cache Size = {}", k, asyncCache.synchronous().asMap().size());
}
field.setAccessible(false); field.setAccessible(false);
} catch (Exception e) { } catch (Exception e) {
logger.warn("this bean : {} , no have method : {}, Lead to it cannot be deserialized , cause: {}", split[0], field.getName() + "Serialize", e); logger.warn("this bean : {} , no have method : {}, Lead to it cannot be deserialized , cause: {}", split[0], field.getName() + "Serialize", e);
...@@ -183,4 +205,6 @@ public class CacheConfig { ...@@ -183,4 +205,6 @@ public class CacheConfig {
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment