package cn.reghao.tnb.message.app.redis.ds;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

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

    public RedisString(RedisTemplate<String, String> redisTemplate) {
        this.stringOps = redisTemplate.opsForValue();
    }

    public void set(String key, String value) {
        stringOps.set(key, value);
    }

    public void setWithTimeout(String key, String value, long timeout) {
        stringOps.set(key, value, timeout, TimeUnit.SECONDS);
    }

    public Boolean setIfAbsent(String key, String value, long timeout) {
        return stringOps.setIfAbsent(key, value, timeout, TimeUnit.SECONDS);
    }

    public String get(String key) {
        return stringOps.get(key);
    }

    public long incr(String key) {
        Long ret = stringOps.increment(key);
        return ret != null ? ret : 0;
    }

    public long decr(String key) {
        Long ret = stringOps.decrement(key);
        return ret != null ? ret : 0;
    }
}
