package com.ycwl.basic.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @date 2022年08月08日 16:06
 * redis缓存操作行为工具类
 */
@Component
public class RedisCacheOperationUtils {

    /**
     * string类型的redis操作类
     */
    private static StringRedisTemplate stringRedisTemplate;
    /**
     * 通用的redis操作类
     */
    private static RedisTemplate redisTemplate;

    /**
     * 创建string类型的操作对象
     */
    public static BoundValueOperations<String, String> createValueOperations(String cacheKey) {
        return stringRedisTemplate.boundValueOps(cacheKey);
    }
    /**
     * 创建list类型的操作对象
     */
    public static BoundListOperations<String, List<String>> createListOperations(String cacheKey) {
        return redisTemplate.boundListOps(cacheKey);
    }
    /**
     * 创建hash类型的操作对象
     */
    public static BoundHashOperations createHashOperations(String cacheKey) {
        return redisTemplate.boundHashOps(cacheKey);
    }
    /**
     * 创建set类型的操作对象
     */
    public static BoundSetOperations createSetOperations(String cacheKey) {
        return redisTemplate.boundSetOps(cacheKey);
    }
    /**
     * 创建zset类型的操作对象
     */
    public static BoundZSetOperations createZSetOperations(String cacheKey) {
        return redisTemplate.boundZSetOps(cacheKey);
    }
    /**
     * 判断缓存key值是否存在
     */
    public static Boolean hasKey(String cacheKey) {
        return redisTemplate.hasKey(cacheKey);
    }
    /**
     * 删除缓存的redis缓存key
     */
    public static Boolean delete(String cacheKey) {
        return redisTemplate.delete(cacheKey);
    }

    @Autowired
    public void setStringRedisTemplate(StringRedisTemplate stringRedisTemplate) {
        RedisCacheOperationUtils.stringRedisTemplate = stringRedisTemplate;
    }

    @Autowired
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        RedisCacheOperationUtils.redisTemplate = redisTemplate;
    }
}
