package com.ruoyi.common.third.cache;

import com.ruoyi.common.third.utils.SpringContextUtil;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.Optional;


public abstract class AbstractCacheOperator {
    private static HzCacheManager getCacheManager() {
        Optional<HzCacheManager> bean = Optional.ofNullable(SpringContextUtil.getBean(HzCacheManager.class));
        if (!bean.isPresent()) {
            throw new NoCacheManagerException("获取缓存对象失败！");
        }
        return bean.get();
    }

    protected static HzCache getCache(String cacheName) {
        // 增加 判断缓存名称为空的判断
        if (StringUtils.isEmpty(cacheName)) {
            throw new NoCacheManagerException("缓存的名称不能为空");
        }
        return getCacheManager().getCache(cacheName);
    }

    /**
     * 获取cachename
     *
     * @date 2021年11月1日10:32:31
     **/
    public static Collection<String> getCacheNames() {
        return getCacheManager().getCacheNames();
    }
}
