package com.ruoyi.hwMeeting.util;

import com.ruoyi.common.utils.CacheUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class hwMeetingCacheUtil {

    private static Logger logger = LoggerFactory.getLogger(CacheUtils.class);

    private static CacheManager cacheManager = SpringUtils.getBean(CacheManager.class);

    private static final String CACHE = "hwMeetingCache";

    /**
     * 获取CACHE缓存
     *
     * @param key
     * @return
     */
    public static Object get(String key)
    {
        return get(CACHE, key);
    }

    /**
     * 获取缓存
     *
     * @param cacheName
     * @param key
     * @return
     */
    private static Object get(String cacheName, String key)
    {
        return getCache(cacheName).get(key);
    }

    /**
     * 写入CACHE缓存
     *
     * @param key
     * @return
     */
    public static void put(String key, Object value)
    {
        put(CACHE, key, value);
    }


    /**
     * 写入缓存
     *
     * @param cacheName
     * @param key
     * @param value
     */
    private static void put(String cacheName, String key, Object value)
    {
        getCache(cacheName).put(key, value);
    }

    /**
     * 判断是否有CACHE缓存
     *
     * @param key
     * @return
     */
    public static boolean has(String key)
    {
        Object o = getCache(CACHE).get(key);
        if(o == null){
            return false;
        }
        return true;
    }


    /**
     * 获得一个Cache，没有则显示日志。
     *
     * @param cacheName
     * @return
     */
    private static Cache<String, Object> getCache(String cacheName)
    {
        Cache<String, Object> cache = cacheManager.getCache(cacheName);
        if (cache == null)
        {
            throw new RuntimeException("当前系统中没有定义“" + cacheName + "”这个缓存。");
        }
        return cache;
    }
}
