using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UtilsSharp.Redis;

namespace COMMON
{
    public class XC_Redis
    {

        public static XC_Redis Redis = new XC_Redis();
        private XC_Redis()
        {
            RedisCacheHelper.Initialization(new CSRedis.CSRedisClient(ConfigEntity.Instance.Redis));
        }
        public string fittleKey(string name)
        {
          return  name.ToLower();
        }
        public T GetKey<T>(string name)  
        {
            name = fittleKey(name);
            if (!string.IsNullOrEmpty(name) && RedisCacheHelper.IsExists(name))
            {
                return RedisCacheHelper.Get<T>(name);
            }
            else
            {
                return default(T);
            }
        }

        public bool SetKey<T>(string name, T VAL,int TIME = -1)
        {
            name = fittleKey(name);
            return RedisCacheHelper.Set(name, VAL, TIME);
        }

        public T GET<T>(string name, Func<T> func, int TIME = -1)
        {
            name = fittleKey(name);
            var DATA = this.GetKey<T>(name);
            if (!RedisCacheHelper.IsExists(name) || DATA == null)
            {
                var data = func();
                SetKey(name,data, TIME);
                return data;
            }
            return DATA;
        }
        public bool Remove(string name)
        {
            name = fittleKey(name);
            if (string.IsNullOrEmpty(name) || !RedisCacheHelper.IsExists(name) || RedisCacheHelper.Remove(name)>0)
            {
                return true;
            }
            return false;
        }

        public bool IsExists(string name)
        {
            name = fittleKey(name);
            if (string.IsNullOrEmpty(name) || !RedisCacheHelper.IsExists(name))
            {
                return false;
            }
            return true;
        }
    }
}
