AbsBaseRpcService.java 3.41 KB
package cn.fw.hestia.rpc;

import cn.fw.hestia.common.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

/**
 * @author : kurisu
 * @className : AbsBaseRpcService
 * @description : 公共方法
 * @date: 2020-12-17 14:13
 */
@Slf4j
public abstract class AbsBaseRpcService {
    /**
     * Redis工具
     */
    @Autowired
    protected StringRedisTemplate redisTemplate;

    /**
     * 缓存KEY前缀
     *
     * @return
     */
    protected abstract String getKeyPrefix();

    /**
     * 从缓存获取对象
     *
     * @param key
     * @param clazz
     * @param <E>   获取的对象
     * @return
     */
    @Nullable
    protected <E> E getFromCache(@NonNull final String key, Class<E> clazz) {
        String cache = getFromCache(key);
        if (StringUtils.isEmpty(cache)) {
            return null;
        }
        return JSON.parseObject(cache, clazz);
    }

    protected String getFromCache(@NonNull final String key) {
        String json = null;
        try {
            BoundValueOperations<String, String> ops = redisTemplate.boundValueOps(key);
            json = ops.get();
        } catch (Exception e) {
            log.error("从缓存获信息失败[{}]", key, e);
        }
        return json;
    }

    protected void setToCache(@NonNull final String key, @NonNull final String value) {
        setToCache(key, value, 30);
    }

    /**
     * 缓存信息
     * @param key
     * @param value
     * @param timeout 缓存时间(秒)
     */
    protected void setToCache(@NonNull final String key, @NonNull final String value, long timeout) {
        try {
            redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
        } catch (Exception e) {
            log.error("缓存信息失败[{}][{}]", key, value, e);
        }
    }

    protected <E> List<E> getListFromCache(final String key, Class<E> clazz) {
        try {
            BoundListOperations<String, String> queue = getQueue(key);
            final long size = Optional.ofNullable(queue.size()).orElse(0L);
            if (size > 0) {
                final List<E> dtos = new ArrayList<>();
                for (long i = 0; i < size; i++) {
                    final String json = Objects.requireNonNull(queue.index(i));
                    dtos.add(JSONObject.parseObject(json, clazz));
                }
                return dtos;
            }
        } catch (Exception e) {
            log.error("从缓存获取信息失败[{}]", key, e);
        }
        return null;
    }

    protected void setListToCache(@NonNull final String key, @NonNull final String value) {
        try {
            getQueue(key).rightPush(value);
        } catch (Exception e) {
            log.error("缓存息失败[{}][{}]", key, value, e);
        }
    }

    protected BoundListOperations<String, String> getQueue(@NonNull final String key) {
        return redisTemplate.boundListOps(key);
    }
}