package com.muyu.common.cache;

import com.muyu.common.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.concurrent.TimeUnit;

/**
 * 抽象缓存层
 *  * @className: CacheAbsBasic  ️✈️
 *  * @author: Yang 鹏  🦅
 *  * @date: 2024/9/29 16:08 ⏰
 *  * @Version: 1.0
 *  * @description:
 */
public abstract class CacheAbsBasic <K, V> implements CacheBasic<K, V>{

    @Autowired
    private RedisService redisService;

    @Override
    public void put(K key, V value) {

        try {
            redisService.setCacheObject(encode(key), value);
        } catch (Exception e) {
            throw new RuntimeException("运行时异常，异常信息为：{}"+e.getMessage());
        }
    }

    @Override
    public V get(K key) {

        try {
            return redisService.getCacheObject(encode(key));
        } catch (Exception e) {
            throw new RuntimeException("运行时异常，异常信息为：{}"+e.getMessage());
        }
    }

    @Override
    public void remove(K key) {

        try {
            redisService.deleteObject(encode(key));
        } catch (Exception e) {
            throw new RuntimeException("运行时异常，异常信息为：{}"+e.getMessage());
        }
    }

    @Override
    public boolean hashKey(K key){
        Boolean b = false;

        try {
            b = redisService.hasKey(encode(key));
        } catch (Exception e) {
            throw new RuntimeException("运行时异常，异常信息为：{}"+e.getMessage());
        }
        return b;
    }
}
