package com.tanpu.community.cache;

import com.fasterxml.jackson.core.type.TypeReference;
import com.tanpu.common.redis.RedisHelper;
import com.tanpu.common.util.JsonUtil;
import com.tanpu.community.util.SpringUtils;
import org.apache.commons.lang3.StringUtils;

import java.time.Duration;
import java.util.function.Supplier;

public class RedisCache {

    private String cacheName;
    private RedisHelper redisHelper;

    public <T> T getObject(String key, Integer expireSeconds, Supplier<T> func, Class<T> clz) {
        String value = get(key);
        // todo 考虑缓存穿透的问题.
        if (StringUtils.isNotBlank(value)) {
            return JsonUtil.toBean(value, clz);
        }

        T ret = func.get();
        if (ret != null) {
            put(key, ret, expireSeconds);
        }
        return ret;
    }

    public<T> T getList(String key, Integer expireSeconds, Supplier<T> func, TypeReference<T> ref) {
        String value = get(key);
        if (StringUtils.isNotBlank(value)) {
            return JsonUtil.toBean(value, ref);
        }

        T ret = func.get();
        if (ret != null) {
            put(key, ret, expireSeconds);
        }
        return ret;
    }

    public void evict(String key) {
        key = cacheName + ":" + key;
        delete(key);
    }

    public String get(String key) {
        key = cacheName + ":" + key;
        return redisHelper.get(key);
    }

    public void incr(String key) {
        key = cacheName + ":" + key;
        redisHelper.incr(key);
    }

    public void decr(String key) {
        key = cacheName + ":" + key;
        redisHelper.decr(key);
    }

    public void put(String key, Object obj, Integer expireSeconds) {
        key = cacheName + ":" + key;
        String value = JsonUtil.toJson(obj);
        if (expireSeconds == 0) {
            redisHelper.set(key, value);
        } else {
            redisHelper.set(key, value, Duration.ofSeconds(expireSeconds));
        }
    }

    public void set(String key, String value, Integer expireSeconds) {
        key = cacheName + ":" + key;
        if (expireSeconds == 0) {
            redisHelper.set(key, value);
        } else {
            redisHelper.set(key, value, Duration.ofSeconds(expireSeconds));
        }
    }

    public boolean setIfAbsent(String key, String value, Integer expireSeconds) {
        key = cacheName + ":" + key;
        if (expireSeconds == 0) {
            return redisHelper.setIfAbsent(key, value);
        } else {
            return redisHelper.setIfAbsent(key, value, Duration.ofSeconds(expireSeconds));
        }
    }

    private void delete(String key) {
        redisHelper.delete(key);
    }

    public static class Builder {
        RedisCache cache = new RedisCache();

        public Builder cacheName(String cacheName) {
            cache.cacheName = cacheName;
            cache.redisHelper = SpringUtils.getBean(RedisHelper.class);
            return this;
        }

        public RedisCache build() {
            return cache;
        }
    }
}