package cn.ey.biz.module.system.dal.redis.file;

import cn.ey.biz.module.system.dal.redis.RedisKeyConstants;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.time.Duration;

@Repository
public class FileDAO {

    @Resource
    private RedisTemplate redisTemplate;

    public void set(Long configId, String path, String value) {
        redisTemplate.opsForValue().set(formatKey(configId, path), value, Duration.ofMinutes(1));
    }

    public String get(Long configId, String path) {
        Object uri = redisTemplate.opsForValue().get(formatKey(configId, path));
        if(uri == null){
            return null;
        }
        return uri.toString();
    }

    public void delete(Long configId, String path) {
        redisTemplate.delete(formatKey(configId, path));
    }

    private static String formatKey(Long configId, String path) {
        return String.format(RedisKeyConstants.FILE.getKeyTemplate(), configId, path);
    }

}
