WCS
dengjunjie
2024-10-14 966d1fe6077c885db064fcea98bb48cbccb464d6
项目代码/WMS/WIDESEA_WMSServer/WIDESEA_Core/Caches/SqlSugarCacheService.cs
对比新文件
@@ -0,0 +1,69 @@
锘縰sing 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
{
    /// <summary>
    /// 瀹炵幇SqlSugar鐨処CacheService鎺ュ彛
    /// </summary>
    public class SqlSugarCacheService : ICacheService
    {
        private readonly Lazy<ICaching> _caching = new(() => App.GetService<ICaching>(false));
        private ICaching Caching => _caching.Value;
        public void Add<V>(string key, V value)
        {
            Caching.Set(key, value);
        }
        public void Add<V>(string key, V value, int cacheDurationInSeconds)
        {
            Caching.Set(key, value, TimeSpan.FromSeconds(cacheDurationInSeconds));
        }
        public bool ContainsKey<V>(string key)
        {
            return Caching.Exists(key);
        }
        public V Get<V>(string key)
        {
            return Caching.Get<V>(key);
        }
        public IEnumerable<string> GetAllKey<V>()
        {
            return Caching.GetAllCacheKeys();
        }
        public V GetOrCreate<V>(string cacheKey, Func<V> create, int cacheDurationInSeconds = int.MaxValue)
        {
            if (!ContainsKey<V>(cacheKey))
            {
                var value = create();
                Caching.Set(cacheKey, value, TimeSpan.FromSeconds(cacheDurationInSeconds));
                return value;
            }
            return Caching.Get<V>(cacheKey);
        }
        public void Remove<V>(string key)
        {
            Caching.Remove(key);
        }
        public bool RemoveAll()
        {
            Caching.RemoveAll();
            return true;
        }
    }
}