package cn.reghao.tnb.content.app.util.redis.ds;

import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

/**
 * @author reghao
 * @date 2021-03-07 17:44:23
 */
@Component
public class RedisKey {
    private final RedisOperations<String, String> redisOps;

    public RedisKey(RedisTemplate<String, String> redisTemplate) {
        this.redisOps = redisTemplate;
    }

    public Long eval(String lua, String[] keys, Object... values) {
        DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>(lua, Long.class);
        return redisOps.execute(redisScript, Arrays.asList(keys), values);
    }

    public Boolean expire(String key, long timeout) {
        return redisOps.expire(key, timeout, TimeUnit.SECONDS);
    }

    public Boolean exists(String key) {
        return false;
    }
}
