
package com.tmerclub.cloud.common.cache.util;

import com.alibaba.fastjson2.JSON;
import com.tmerclub.cloud.common.cache.config.RedisCacheConfig;
import com.tmerclub.cloud.common.cache.constant.CacheNames;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Component;

/**
 * @author tmerclub
 * @date 2020/09/03
 */
@Component
public class CacheManagerUtil {

    private final CacheManager cacheManager;

    @Autowired
    public CacheManagerUtil(CacheManager cacheManager) {
        this.cacheManager = cacheManager;
    }

    public <T> T getCache(String cacheName, String key, Class<T> clazz) {
        String data = RedisUtil.get(cacheName + CacheNames.UNION + key);
        return JSON.parseObject(data, clazz);
    }

    public void putCache(String cacheName, String key, Object value) {
        RedisUtil.set(cacheName + CacheNames.UNION + key, JSON.toJSONString(value), RedisCacheConfig.SECONDS);
    }

    public void evictCache(String cacheName, String key) {
        RedisUtil.del(cacheName + CacheNames.UNION + key);
    }
}
