using Microsoft.Extensions.Caching.Memory;
using System.Collections.Generic;
using System;

namespace HTCoreServiceApp.Common
{
    public class MemoryCacheHelper: IMemoryCacheHelper
    {
        private readonly IMemoryCache cache;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="cache"></param>
        public MemoryCacheHelper(IMemoryCache cache)
        {
            this.cache = cache;
        }

        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <typeparam name="TValue">缓存值类型</typeparam>
        /// <param name="key">键</param>
        /// <returns>缓存值</returns>
        public TValue Get<TValue>(string key)
        {
            return cache.Get<TValue>(key);
        }

        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <typeparam name="TValue">缓存值类型</typeparam>
        /// <param name="keys">键集合</param>
        /// <returns>缓存值集合</returns>
        public List<TValue> Get<TValue>(List<string> keys)
        {
            List<TValue> list = new List<TValue>();
            foreach (string key in keys)
            {
                list.Add(cache.Get<TValue>(key));
            }
            return list;
        }

        /// <summary>
        /// 设置缓存
        /// </summary>
        /// <typeparam name="TValue">缓存值类型</typeparam>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <param name="expires">过期时间</param>
        /// <param name="isSliding">是否滑动过期</param>
        /// <returns>是否成功</returns>
        public bool Set<TValue>(string key, TValue value, int expires = 0, bool isSliding = false)
        {
            try
            {
                if (expires == 0)
                {
                    cache.Set(key, value);
                }
                else
                {
                    if (isSliding)
                    {
                        cache.Set(key, value, new MemoryCacheEntryOptions
                        {
                            SlidingExpiration = TimeSpan.FromSeconds(expires)
                        });
                    }
                    else
                    {
                        cache.Set(key, value, new MemoryCacheEntryOptions
                        {
                            AbsoluteExpiration = DateTime.Now.AddSeconds(expires)
                        });
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 判断缓存是否存在
        /// </summary>
        /// <param name="key">键</param>
        /// <returns>是否存在</returns>
        public bool IsExist(string key)
        {
            if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key))
            {
                return false;
            }
            else
            {
                return cache.TryGetValue(key, out _);
            }
        }

        /// <summary>
        /// 移除缓存
        /// </summary>
        /// <param name="key">键</param>
        /// <returns>是否成功</returns>
        public bool Remove(string key)
        {
            try
            {
                cache.Remove(key);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 移除缓存
        /// </summary>
        /// <param name="keys">键集合</param>
        /// <returns>是否成功</returns>
        public bool Remove(List<string> keys)
        {
            try
            {
                foreach (string key in keys)
                {
                    cache.Remove(key);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
