|
|
package com.yoho.search.cache.beans;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.alibaba.fastjson.TypeReference;
|
|
|
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
|
|
|
import com.yoho.search.cache.impls.EhCache;
|
|
|
import com.yoho.search.cache.impls.SearchRedis;
|
...
|
...
|
@@ -13,7 +10,6 @@ import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
|
import java.lang.reflect.ParameterizedType;
|
|
|
import java.lang.reflect.Type;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
public abstract class AbstractCacheComponent<T> {
|
...
|
...
|
@@ -23,7 +19,7 @@ public abstract class AbstractCacheComponent<T> { |
|
|
@Autowired
|
|
|
private SearchRedis searchRedis;
|
|
|
|
|
|
public T queryWithCache(ParamQueryFilter paramQueryFilter,Map<String,String> paramMap) {
|
|
|
public T queryWithCache(ParamQueryFilter paramQueryFilter, Map<String, String> paramMap) throws Exception {
|
|
|
//1、生成RedisKeyBuilder
|
|
|
RedisKeyBuilder redisKeyBuilder = this.genRedisKeyBuilder(paramQueryFilter);
|
|
|
if (redisKeyBuilder == null) {
|
...
|
...
|
@@ -40,11 +36,11 @@ public abstract class AbstractCacheComponent<T> { |
|
|
this.addValueToEhcache(redisKeyBuilder, result);
|
|
|
return result;
|
|
|
}
|
|
|
return this.doInnerQuery(redisKeyBuilder, paramQueryFilter,paramMap);
|
|
|
return this.doInnerQuery(redisKeyBuilder, paramQueryFilter, paramMap);
|
|
|
}
|
|
|
|
|
|
private T doInnerQuery(RedisKeyBuilder redisKeyBuilder, ParamQueryFilter paramQueryFilter,Map<String,String> paramMap) {
|
|
|
T result = this.doRealQuery(paramQueryFilter,paramMap);
|
|
|
private T doInnerQuery(RedisKeyBuilder redisKeyBuilder, ParamQueryFilter paramQueryFilter, Map<String, String> paramMap) throws Exception {
|
|
|
T result = this.doRealQuery(paramQueryFilter, paramMap);
|
|
|
if (result == null) {
|
|
|
return result;
|
|
|
}
|
...
|
...
|
@@ -57,8 +53,10 @@ public abstract class AbstractCacheComponent<T> { |
|
|
return result;
|
|
|
}
|
|
|
|
|
|
private T getValueFromEhcache(RedisKeyBuilder redisKeyBuilder) {
|
|
|
if(!useEhcache()){
|
|
|
protected abstract T doRealQuery(ParamQueryFilter paramQueryFilter, Map<String, String> paramMap) throws Exception;
|
|
|
|
|
|
private T getValueFromEhcache(RedisKeyBuilder redisKeyBuilder) throws Exception {
|
|
|
if (!useEhcache()) {
|
|
|
return null;
|
|
|
}
|
|
|
CacheObject cacheObject = ehCache.get(redisKeyBuilder);
|
...
|
...
|
@@ -74,14 +72,14 @@ public abstract class AbstractCacheComponent<T> { |
|
|
}
|
|
|
|
|
|
private T getValueFromRedis(RedisKeyBuilder redisKeyBuilder) {
|
|
|
if(!useRedis()){
|
|
|
if (!useRedis()) {
|
|
|
return null;
|
|
|
}
|
|
|
CacheObject cacheObject = searchRedis.get(redisKeyBuilder);
|
|
|
if (cacheObject == null) {
|
|
|
return null;
|
|
|
}
|
|
|
String redisValue = (String)cacheObject.toObject();
|
|
|
String redisValue = (String) cacheObject.toObject();
|
|
|
Type superClass = getClass().getGenericSuperclass();
|
|
|
Type type = ((ParameterizedType) superClass).getActualTypeArguments()[0];
|
|
|
return JSON.parseObject(redisValue, type);
|
...
|
...
|
@@ -97,8 +95,6 @@ public abstract class AbstractCacheComponent<T> { |
|
|
|
|
|
protected abstract int cacheTimeInMinute();
|
|
|
|
|
|
protected abstract T doRealQuery(ParamQueryFilter paramQueryFilter,Map<String,String> paramMap);
|
|
|
|
|
|
protected boolean useEhcache() {
|
|
|
return false;
|
|
|
}
|
...
|
...
|
|