AbstractPageComponent.java 3.15 KB
package com.yoho.search.cache.beans;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
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.recall.models.common.ParamQueryFilter;
import org.springframework.beans.factory.annotation.Autowired;

public abstract class AbstractPageComponent<T> {

    @Autowired
    private EhCache ehCache;
    @Autowired
    private SearchRedis searchRedis;

    public T queryWithCache(ParamQueryFilter paramQueryFilter, Class<T> clazz) {
        //1、生成RedisKeyBuilder
        RedisKeyBuilder redisKeyBuilder = this.genRedisKeyBuilder(paramQueryFilter);
        if (redisKeyBuilder == null) {
            return null;
        }
        //2、取本地缓存,缓存命中,则直接返回
        T result = this.getJavaObjectFromEhcache(redisKeyBuilder, clazz);
        if (result != null) {
            return result;
        }
        //3、取redis缓存,缓存命中,则回写ehcahce
        result = this.getJavaObjectFromRedis(redisKeyBuilder, clazz);
        if (result != null) {
            this.addJavaObjectToEhcache(redisKeyBuilder, result);
            return result;
        }
        return this.doInnerQuery(redisKeyBuilder, paramQueryFilter);
    }

    private T doInnerQuery(RedisKeyBuilder redisKeyBuilder, ParamQueryFilter paramQueryFilter) {
        T result = this.doRealQuery(paramQueryFilter);
        this.addJavaObjectToEhcache(redisKeyBuilder, result);
        this.addJavaObjectToRedis(redisKeyBuilder, result);
        return result;
    }

    private T getJavaObjectFromEhcache(RedisKeyBuilder redisKeyBuilder, Class<T> clazz) {
        CacheObject cacheObject = ehCache.get(redisKeyBuilder);
        if (cacheObject != null) {
            Object object = cacheObject.toObject();
            return (T) object;
        }
        return null;
    }

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

    protected T getJavaObjectFromRedis(RedisKeyBuilder redisKeyBuilder, Class<T> clazz) {
        CacheObject cacheObject = searchRedis.get(redisKeyBuilder);
        if (cacheObject == null) {
            return null;
        }
        JSONObject jsonObject = cacheObject.toJSONObject();
        T result = JSON.toJavaObject(jsonObject, clazz);
        return result;
    }

    protected void addJavaObjectToRedis(RedisKeyBuilder redisKeyBuilder, T result) {
        JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(result));
        CacheObject toCacheResult = new CacheObject(jsonObject);
        searchRedis.addOrUpdate(redisKeyBuilder, toCacheResult, this.cacheTimeInMinute());
    }

    protected abstract RedisKeyBuilder genRedisKeyBuilder(ParamQueryFilter paramQueryFilter);

    protected abstract int cacheTimeInMinute();

    protected abstract T doRealQuery(ParamQueryFilter paramQueryFilter);

}