package com.ruoyi.common.core.redis;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.springframework.stereotype.Component;
import com.ruoyi.common.core.cache.InMemoryCacheStats;
import com.ruoyi.common.core.cache.InMemoryCacheStore;

/**
 * 本地缓存门面，保留原有 RedisCache 业务入口。
 *
 * @author ruoyi
 **/
@SuppressWarnings("unchecked")
@Component
public class RedisCache
{
    private final InMemoryCacheStore cacheStore;

    public RedisCache(InMemoryCacheStore cacheStore)
    {
        this.cacheStore = cacheStore;
    }

    public <T> void setCacheObject(final String key, final T value)
    {
        cacheStore.set(key, value);
    }

    public <T> void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit)
    {
        cacheStore.set(key, value, timeout.longValue(), timeUnit);
    }

    public boolean expire(final String key, final long timeout)
    {
        return expire(key, timeout, TimeUnit.SECONDS);
    }

    public boolean expire(final String key, final long timeout, final TimeUnit unit)
    {
        return cacheStore.expire(key, timeout, unit);
    }

    public long getExpire(final String key)
    {
        return cacheStore.getExpire(key);
    }

    public Boolean hasKey(String key)
    {
        return cacheStore.hasKey(key);
    }

    public <T> T getCacheObject(final String key)
    {
        return cacheStore.get(key);
    }

    public boolean deleteObject(final String key)
    {
        return cacheStore.delete(key);
    }

    public boolean deleteObject(final Collection collection)
    {
        if (collection == null || collection.isEmpty())
        {
            return false;
        }
        List<String> keys = new ArrayList<>(collection.size());
        for (Object item : collection)
        {
            keys.add(String.valueOf(item));
        }
        return cacheStore.delete(keys);
    }

    public <T> long setCacheList(final String key, final List<T> dataList)
    {
        cacheStore.putList(key, dataList);
        return dataList == null ? 0 : dataList.size();
    }

    public <T> List<T> getCacheList(final String key)
    {
        return cacheStore.getList(key);
    }

    public <T> long setCacheSet(final String key, final Set<T> dataSet)
    {
        cacheStore.putSet(key, dataSet);
        return dataSet == null ? 0 : dataSet.size();
    }

    public <T> Set<T> getCacheSet(final String key)
    {
        return cacheStore.getSet(key);
    }

    public <T> void setCacheMap(final String key, final Map<String, T> dataMap)
    {
        if (dataMap != null)
        {
            cacheStore.putMap(key, dataMap);
        }
    }

    public <T> Map<String, T> getCacheMap(final String key)
    {
        return cacheStore.getMap(key);
    }

    public <T> void setCacheMapValue(final String key, final String hKey, final T value)
    {
        cacheStore.putMapValue(key, hKey, value);
    }

    public <T> T getCacheMapValue(final String key, final String hKey)
    {
        Map<String, T> map = cacheStore.getMap(key);
        return map == null ? null : map.get(hKey);
    }

    public <T> List<T> getMultiCacheMapValue(final String key, final Collection<Object> hKeys)
    {
        Map<String, T> map = cacheStore.getMap(key);
        if (map == null || hKeys == null || hKeys.isEmpty())
        {
            return Collections.emptyList();
        }
        List<T> values = new ArrayList<>(hKeys.size());
        for (Object hKey : hKeys)
        {
            values.add(map.get(String.valueOf(hKey)));
        }
        return values;
    }

    public boolean deleteCacheMapValue(final String key, final String hKey)
    {
        return cacheStore.deleteMapValue(key, hKey);
    }

    public Collection<String> keys(final String pattern)
    {
        return cacheStore.keys(pattern);
    }

    public long increment(String key, long timeout, TimeUnit unit)
    {
        return cacheStore.increment(key, timeout, unit);
    }

    public InMemoryCacheStats getCacheStats()
    {
        return cacheStore.snapshot();
    }

    public void clear()
    {
        cacheStore.clear();
    }
}
