package cn.reghao.tnb.content.app.util.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 RedisStringObject {
    private final ValueOperations<String, Object> stringOps;

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

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

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

    public Boolean setIfAbsent(String key, Object value) {
        return stringOps.setIfAbsent(key, value);
    }

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

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