package com.kxs.adminap.util;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import jakarta.annotation.Resource;

@Component
public class RedisUtils {
 
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

	public void SetRedisUtils()
	{
		//设置序列化Key的实例化对象
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		//设置序列化Value的实例化对象
		redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
	}
 
    /**
     * 读取缓存
     *
     * @param key
     * @return
     */
    public String get(final String key) {
		SetRedisUtils();
		Object obj = redisTemplate.opsForValue().get(key);
        return obj == null ? "" : obj.toString();
    }
	public Integer getInteger(final String key) {
		SetRedisUtils();
		Object obj = redisTemplate.opsForValue().get(key);
        return obj == null ? 0 : (Integer)obj;
    }
 
    /**
     * 写入缓存
     */
    public boolean set(final String key, Object value) {
		SetRedisUtils();
        boolean result = false;
        try {
            redisTemplate.opsForValue().set(key, value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
 
    /**
     * 写入缓存,并设置过期时间
     *
     * @param key
     * @param value
     * @param timeout
     * @param unit
     * @return
     */
    public boolean set(final String key, String value, long timeout, TimeUnit unit) {
        boolean result = false;
        try {
            redisTemplate.opsForValue().set(key, value, timeout, unit);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
 
    /**
     * 更新缓存
     */
    public boolean getAndSet(final String key, String value) {
        boolean result = false;
        try {
            redisTemplate.opsForValue().getAndSet(key, value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
 
    /**
     * 删除缓存
     */
    public boolean delete(final String key) {
        boolean result = false;
        try {
            redisTemplate.delete(key);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

	public Long addList(final String key, Object value) {
		SetRedisUtils();
        Long result = 0L;
        try {
            result = redisTemplate.opsForList().rightPush(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

	public List<Object> getList(final String key) {
		SetRedisUtils();
        List<Object> result = null;
        try {
            result = redisTemplate.opsForList().range(key, 0, -1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}