namespace WIDESEA_Cache;
/// 
/// 
/// 内存缓存
/// 
public partial class MemoryCacheService : ISimpleCacheService
{
    /// 
    public void HashAdd(string key, string hashKey, T value)
    {
        //获取字典
        var exist = _memoryCache.GetDictionary(key);
        if (exist.ContainsKey(hashKey))//如果包含Key
            exist[hashKey] = value;//重新赋值
        else exist.Add(hashKey, value);//加上新的值
        _memoryCache.Set(key, exist);
    }
    //private IDictionary GetDictionary(string key,string)
    /// 
    public bool HashSet(string key, Dictionary dic)
    {
        //获取字典
        var exist = _memoryCache.GetDictionary(key);
        dic.ForEach(it =>
        {
            if (exist.ContainsKey(it.Key))//如果包含Key
                exist[it.Key] = it.Value;//重新赋值
            else exist.Add(it.Key, it.Value);//加上新的值
        });
        return true;
    }
    /// 
    public int HashDel(string key, params string[] fields)
    {
        int result = 0;
        //获取字典
        var exist = _memoryCache.GetDictionary(key);
        foreach (var field in fields)
        {
            if (field != null && exist.ContainsKey(field))//如果包含Key
            {
                exist.Remove(field);//删除
                result++;
            }
        }
        return result;
    }
    /// 
    public List HashGet(string key, params string[] fields)
    {
        List list = new List();
        //获取字典
        var exist = _memoryCache.GetDictionary(key);
        foreach (var field in fields)
        {
            if (exist.ContainsKey(field))//如果包含Key
            {
                list.Add(exist[field]);
            }
            else { list.Add(default); }
        }
        return list;
    }
    /// 
    public T HashGetOne(string key, string field)
    {
        //获取字典
        var exist = _memoryCache.GetDictionary(key);
        exist.TryGetValue(field, out T result);
        var data = result.DeepClone();
        return data;
    }
    /// 
    public IDictionary HashGetAll(string key)
    {
        var data = _memoryCache.GetDictionary(key);
        return data;
    }
}