spring data redis操作redis工具类

private Logger logger = LoggerFactory.getLogger(this.getClass());
    private static Logger cacheLogger = LoggerFactory.getLogger("unionCachePerf4j");

    private final static String EMPTY_VALUE = "null";

    @Autowired
    @Qualifier("union_template")
    private RedisTemplate redisTemplate;

    private ValueOperations stringOperations;

    private HashOperations hashOperations;

    private ListOperations listOperations;

    @PostConstruct
    public void init() {
        stringOperations = redisTemplate.opsForValue();
        hashOperations = redisTemplate.opsForHash();
        listOperations = redisTemplate.opsForList();
    }

    /**
     * 缓存存入空值
     *
     * @param key
     * @param time
     * @param timeUnit
     */
    public void writeEmpty(String key, long time, TimeUnit timeUnit) {
        StopWatch stopWatch = new Slf4JStopWatch(cacheLogger);
        logger.debug("返回值为null,缓存空值,key={},time={},timeUnit={}", key, time, timeUnit);
        stringOperations.set(key, "", time, timeUnit);
        logger.debug("空值写入成功,key={}", key);
        stopWatch.lap("redis.writeEmpty");
    }

    /**
     * 写入缓存
     *
     * @param key
     * @param value
     * @param time
     * @param timeUnit
     */
    public void writeCache(String key, String value, long time, TimeUnit timeUnit) {
        StopWatch stopWatch = new Slf4JStopWatch(cacheLogger);
        logger.debug("写入缓存,key={},value={},time={},timeUnit={}", key, value, time, timeUnit);
        stringOperations.set(key, value, time, timeUnit);
        logger.debug("缓存写入成功,key={}", key);
        stopWatch.lap("redis.writeCache");
    }

    /**
     * 获取缓存值
     *
     * @return
     */
    public String getValue(String key) {
        StopWatch stopWatch = new Slf4JStopWatch(cacheLogger);
        String value = stringOperations.get(key);
        stopWatch.lap("redis.getValue");
        return value;
    }

    /**
     * 存入hash
     *
     * @param key
     * @param value
     * @param 
     */
    public  void writeHash(String key, T value) {
        StopWatch stopWatch = new Slf4JStopWatch(cacheLogger);
        Map mappedHash = null;
        try {
            mappedHash = ObjectUtil.convertBean(value);
            stopWatch.lap("redis.writeHash.convertHashToMap");
        } catch (Exception e) {
            e.printStackTrace();
        }
        hashOperations.putAll(key, mappedHash);
        stopWatch.lap("redis.writeHash");
    }

    /**
     * 缓存hash空值
     *
     * @param key
     * @param time
     * @param timeUnit
     */
    public void writeHashEmpty(String key, long time, TimeUnit timeUnit) {
        StopWatch stopWatch = new Slf4JStopWatch(cacheLogger);
        logger.debug("返回值为null,缓存空值,key={},time={},timeUnit={}", key, time, timeUnit);
        hashOperations.put(key, EMPTY_VALUE, EMPTY_VALUE);
        this.setHashExpireTime(key, time, timeUnit);
        logger.debug("空值写入成功,key={}", key);
        stopWatch.lap("redis.writeHashEmpty");
    }

    /**
     * 读取hash
     *
     * @param key
     * @param beanClass
     * @param 
     * @return
     */
    public  T loadHash(String key, Class beanClass) {
        StopWatch stopWatch = new Slf4JStopWatch(cacheLogger);
        Map loadedHash = hashOperations.entries(key);
        if (loadedHash.isEmpty() || (loadedHash.containsKey(EMPTY_VALUE) && loadedHash.containsValue(EMPTY_VALUE))) {
            return null;
        }
        Object obj = null;
        try {
            obj = ObjectUtil.convertMap(beanClass, loadedHash);
            stopWatch.lap("redis.loadHash.convertHashToObject");
        } catch (Exception e) {
            e.printStackTrace();
        }
        stopWatch.lap("redis.loadHash");
        return (T) obj;
    }

    /**
     * 设置hash类型key的过期时间
     *
     * @param key
     * @param timeOut
     * @param timeUnit
     * @return
     */
    public Boolean setHashExpireTime(String key, long timeOut, TimeUnit timeUnit) {
        StopWatch stopWatch = new Slf4JStopWatch(cacheLogger);
        Boolean result = redisTemplate.boundHashOps(key).expire(timeOut, timeUnit);
        stopWatch.lap("redis.setHashExpireTime");
        return result;
    }

    /**
     * 写入list
     *
     * @param key
     * @param value
     * @param 
     * @return
     */
    public  Long writeList(String key, List value) {
        StopWatch stopWatch = new Slf4JStopWatch(cacheLogger);
        if (!CollectionUtils.isEmpty(value)) {
            return listOperations.leftPushAll(key, value);
        }
        stopWatch.lap("redis.writeList");
        return 0L;
    }

    /**
     * 获取List
     *
     * @param key
     * @param start
     * @param end
     */
    public List loadList(String key, long start, long end) {
        StopWatch stopWatch = new Slf4JStopWatch(cacheLogger);
        List list = listOperations.range(key, start, end);
        stopWatch.lap("redis.loadList");
        return list;
    }

    /**
     * 获取所有列表
     *
     * @param key
     * @return
     */
    public List loadListAll(String key) {
        StopWatch stopWatch = new Slf4JStopWatch(cacheLogger);
        Long size = listOperations.size(key);
        List list = listOperations.range(key, 0, size);
        stopWatch.lap("redis.loadListAll");
        return list;
    }

    /**
     * 设置list过期时间
     *
     * @param key
     * @param timeOut
     * @param timeUnit
     * @return
     */
    public Boolean setListExpireTime(String key, int timeOut, TimeUnit timeUnit) {
        StopWatch stopWatch = new Slf4JStopWatch(cacheLogger);
        Boolean result = redisTemplate.boundListOps(key).expire(timeOut, timeUnit);
        stopWatch.lap("redis.setListExpireTime");
        return result;
    }

你可能感兴趣的:(spring data redis操作redis工具类)