AbstractCacheComponent.java 3.75 KB
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;
import com.yoho.search.cache.model.CacheObject;
import com.yoho.search.service.recall.models.common.ParamQueryFilter;
import org.springframework.beans.factory.annotation.Autowired;

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) {
        //1、生成RedisKeyBuilder
        RedisKeyBuilder redisKeyBuilder = this.genRedisKeyBuilder(paramQueryFilter);
        if (redisKeyBuilder == null) {
            return null;
        }
        //2、取本地缓存,缓存命中,则直接返回
        T result = this.getValueFromEhcache(redisKeyBuilder);
        if (result != null) {
            return result;
        }
        //3、取redis缓存,缓存命中,则回写ehcahce
        result = this.getValueFromRedis(redisKeyBuilder);
        if (result != null && useEhcache()) {
            this.addValueToEhcache(redisKeyBuilder, result);
            return result;
        }
        return this.doInnerQuery(redisKeyBuilder, paramQueryFilter);
    }

    private T doInnerQuery(RedisKeyBuilder redisKeyBuilder, ParamQueryFilter paramQueryFilter) {
        T result = this.doRealQuery(paramQueryFilter);
        if (result == null) {
            return result;
        }
        if (useEhcache()) {
            this.addValueToEhcache(redisKeyBuilder, result);
        }
        if (useRedis()) {
            this.addValueToRedis(redisKeyBuilder, result);
        }
        return result;
    }

    private T getValueFromEhcache(RedisKeyBuilder redisKeyBuilder) {
        if(!useEhcache()){
            return null;
        }
        CacheObject cacheObject = ehCache.get(redisKeyBuilder);
        if (cacheObject != null) {
            Object object = cacheObject.toObject();
            return (T) object;
        }
        return null;
    }

    private void addValueToEhcache(RedisKeyBuilder redisKeyBuilder, T result) {
        ehCache.addOrUpdate(redisKeyBuilder, new CacheObject(result), this.cacheTimeInMinute());
    }

    protected T getValueFromRedis(RedisKeyBuilder redisKeyBuilder) {
        if(!useRedis()){
            return null;
        }
        CacheObject cacheObject = searchRedis.get(redisKeyBuilder);
        if (cacheObject == null) {
            return null;
        }
        String redisValue = (String)cacheObject.toObject();
        Type superClass = getClass().getGenericSuperclass();
        Type type = ((ParameterizedType) superClass).getActualTypeArguments()[0];
        return JSON.parseObject(redisValue, type);
    }

    protected void addValueToRedis(RedisKeyBuilder redisKeyBuilder, T result) {
        String jsonString = JSON.toJSONString(result);
        CacheObject toCacheResult = new CacheObject(jsonString);
        searchRedis.addOrUpdate(redisKeyBuilder, toCacheResult, 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;
    }

}