package com.tanpu.community.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class RedisService {

    // todo prefix


    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    public boolean existsKey(String key) {
        return redisTemplate.hasKey(key);
    }

    /**
     * set
     */
    public void set(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }

    public void set(String key, Object value, long time, TimeUnit timeUnit) {
        redisTemplate.opsForValue().set(key, value, time, timeUnit);
    }

    /**
     * get
     */
    public String getString(String key) {
        Object v = redisTemplate.opsForValue().get(key);
        return v == null ? null : (String) v;
    }

    public Integer getInteger(String key) {
        Object v = redisTemplate.opsForValue().get(key);
        return v == null ? null : (Integer) v;
    }
}