using Microsoft.Extensions.Caching.Memory;
using SqlSugar;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace WIDESEA_Core.Caches
{
    /// 
    /// 实现SqlSugar的ICacheService接口
    /// 
    public class SqlSugarCacheService : ICacheService
    {
        private readonly Lazy _caching = new(() => App.GetService(false));
        private ICaching Caching => _caching.Value;
        public void Add(string key, V value)
        {
            Caching.Set(key, value);
        }
        public void Add(string key, V value, int cacheDurationInSeconds)
        {
            Caching.Set(key, value, TimeSpan.FromSeconds(cacheDurationInSeconds));
        }
        public bool ContainsKey(string key)
        {
            return Caching.Exists(key);
        }
        public V Get(string key)
        {
            return Caching.Get(key);
        }
        public IEnumerable GetAllKey()
        {
            return Caching.GetAllCacheKeys();
        }
        public V GetOrCreate(string cacheKey, Func create, int cacheDurationInSeconds = int.MaxValue)
        {
            if (!ContainsKey(cacheKey))
            {
                var value = create();
                Caching.Set(cacheKey, value, TimeSpan.FromSeconds(cacheDurationInSeconds));
                return value;
            }
            return Caching.Get(cacheKey);
        }
        public void Remove(string key)
        {
            Caching.Remove(key);
        }
        public bool RemoveAll()
        {
            Caching.RemoveAll();
            return true;
        }
    }
}