|
|
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;
|
...
|
...
|
@@ -8,57 +9,57 @@ import com.yoho.search.cache.impls.EhCache; |
|
|
import com.yoho.search.cache.impls.SearchRedis;
|
|
|
import com.yoho.search.cache.model.CacheObject;
|
|
|
import com.yoho.search.service.recall.models.common.ParamQueryFilter;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
|
public abstract class AbstractPageComponent<T> {
|
|
|
import java.lang.reflect.ParameterizedType;
|
|
|
import java.lang.reflect.Type;
|
|
|
import java.util.List;
|
|
|
|
|
|
public abstract class AbstractCacheComponent<T> {
|
|
|
|
|
|
@Autowired
|
|
|
private EhCache ehCache;
|
|
|
@Autowired
|
|
|
private SearchRedis searchRedis;
|
|
|
|
|
|
public T queryWithCache(ParamQueryFilter paramQueryFilter, Class<T> clazz) {
|
|
|
public T queryWithCache(ParamQueryFilter paramQueryFilter) {
|
|
|
//1、生成RedisKeyBuilder
|
|
|
RedisKeyBuilder redisKeyBuilder = this.genRedisKeyBuilder(paramQueryFilter);
|
|
|
if (redisKeyBuilder == null) {
|
|
|
return null;
|
|
|
}
|
|
|
//2、取本地缓存,缓存命中,则直接返回
|
|
|
T result = this.getJavaObjectFromEhcache(redisKeyBuilder, clazz);
|
|
|
T result = this.getValueFromEhcache(redisKeyBuilder);
|
|
|
if (result != null) {
|
|
|
return result;
|
|
|
}
|
|
|
//3、取redis缓存,缓存命中,则回写ehcahce
|
|
|
result = this.getJavaObjectFromRedis(redisKeyBuilder, clazz);
|
|
|
if (result != null) {
|
|
|
this.addJavaObjectToEhcache(redisKeyBuilder, result);
|
|
|
result = this.getValueFromRedis(redisKeyBuilder);
|
|
|
if (result != null && useEhcache()) {
|
|
|
this.addValueToEhcache(redisKeyBuilder, result);
|
|
|
return result;
|
|
|
}
|
|
|
return this.doInnerQuery(redisKeyBuilder, paramQueryFilter);
|
|
|
}
|
|
|
|
|
|
public T queryFromCache(RedisKeyBuilder redisKeyBuilder) {
|
|
|
if (redisKeyBuilder == null) {
|
|
|
return null;
|
|
|
}
|
|
|
T result = this.getJavaObjectFromEhcache(redisKeyBuilder, null);
|
|
|
if (result == null) {
|
|
|
if ((result = this.getFromRedis(redisKeyBuilder, null)) != null) {
|
|
|
this.addJavaObjectToEhcache(redisKeyBuilder, result);
|
|
|
}
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
private T doInnerQuery(RedisKeyBuilder redisKeyBuilder, ParamQueryFilter paramQueryFilter) {
|
|
|
T result = this.doRealQuery(paramQueryFilter);
|
|
|
this.addJavaObjectToEhcache(redisKeyBuilder, result);
|
|
|
this.addJavaObjectToRedis(redisKeyBuilder, result);
|
|
|
if (result == null) {
|
|
|
return result;
|
|
|
}
|
|
|
if (useEhcache()) {
|
|
|
this.addValueToEhcache(redisKeyBuilder, result);
|
|
|
}
|
|
|
if (useRedis()) {
|
|
|
this.addValueToRedis(redisKeyBuilder, result);
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
private T getJavaObjectFromEhcache(RedisKeyBuilder redisKeyBuilder, Class<T> clazz) {
|
|
|
private T getValueFromEhcache(RedisKeyBuilder redisKeyBuilder) {
|
|
|
if(!useEhcache()){
|
|
|
return null;
|
|
|
}
|
|
|
CacheObject cacheObject = ehCache.get(redisKeyBuilder);
|
|
|
if (cacheObject != null) {
|
|
|
Object object = cacheObject.toObject();
|
...
|
...
|
@@ -67,45 +68,42 @@ public abstract class AbstractPageComponent<T> { |
|
|
return null;
|
|
|
}
|
|
|
|
|
|
protected void addJavaObjectToEhcache(RedisKeyBuilder redisKeyBuilder, T result) {
|
|
|
private void addValueToEhcache(RedisKeyBuilder redisKeyBuilder, T result) {
|
|
|
ehCache.addOrUpdate(redisKeyBuilder, new CacheObject(result), this.cacheTimeInMinute());
|
|
|
}
|
|
|
|
|
|
protected T getJavaObjectFromRedis(RedisKeyBuilder redisKeyBuilder, Class<T> clazz) {
|
|
|
protected T getValueFromRedis(RedisKeyBuilder redisKeyBuilder) {
|
|
|
if(!useRedis()){
|
|
|
return null;
|
|
|
}
|
|
|
CacheObject cacheObject = searchRedis.get(redisKeyBuilder);
|
|
|
if (cacheObject == null) {
|
|
|
return null;
|
|
|
}
|
|
|
JSONObject jsonObject = cacheObject.toJSONObject();
|
|
|
T result = JSON.toJavaObject(jsonObject, clazz);
|
|
|
return result;
|
|
|
String redisValue = (String)cacheObject.toObject();
|
|
|
Type superClass = getClass().getGenericSuperclass();
|
|
|
Type type = ((ParameterizedType) superClass).getActualTypeArguments()[0];
|
|
|
return JSON.parseObject(redisValue, type);
|
|
|
}
|
|
|
|
|
|
protected void addJavaObjectToRedis(RedisKeyBuilder redisKeyBuilder, T result) {
|
|
|
JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(result));
|
|
|
CacheObject toCacheResult = new CacheObject(jsonObject);
|
|
|
protected void addValueToRedis(RedisKeyBuilder redisKeyBuilder, T result) {
|
|
|
String jsonString = JSON.toJSONString(result);
|
|
|
CacheObject toCacheResult = new CacheObject(jsonString);
|
|
|
searchRedis.addOrUpdate(redisKeyBuilder, toCacheResult, this.cacheTimeInMinute());
|
|
|
}
|
|
|
|
|
|
//TODO
|
|
|
protected T getFromRedis(RedisKeyBuilder redisKeyBuilder, Class<T> tClass) {
|
|
|
String result = searchRedis.getString(redisKeyBuilder);
|
|
|
if (tClass != null) {
|
|
|
return StringUtils.isBlank(result) ? null : JSON.parseObject(result, tClass);
|
|
|
}
|
|
|
// Type superClass = getClass().getGenericSuperclass();
|
|
|
// Type type = ((ParameterizedType) superClass).getActualTypeArguments()[0];
|
|
|
return StringUtils.isBlank(result) ? null : JSON.parseObject(result, new TypeReference<T>(){});
|
|
|
}
|
|
|
|
|
|
protected void addToRedis(RedisKeyBuilder redisKeyBuilder, String result) {
|
|
|
searchRedis.addOrUpdate(redisKeyBuilder, result, this.cacheTimeInMinute());
|
|
|
}
|
|
|
|
|
|
protected abstract RedisKeyBuilder genRedisKeyBuilder(ParamQueryFilter paramQueryFilter);
|
|
|
|
|
|
protected abstract int cacheTimeInMinute();
|
|
|
|
|
|
protected abstract T doRealQuery(ParamQueryFilter paramQueryFilter);
|
|
|
|
|
|
protected boolean useEhcache() {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
protected boolean useRedis() {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|