using CoreCms.Net.Utility; using EasyCaching.Core; using SqlSugar; using System; using System.Collections.Generic; namespace CoreCms.Net.Caching.SqlSugar { /// /// 仅供ORM缓存使用 /// public class SqlSugarCache : ICacheService { private static readonly IEasyCachingProvider cache = Storage.GetService(); public void Add(string key, V value) { cache.Set(key, value, TimeSpan.FromSeconds(int.MaxValue)); } public void Add(string key, V value, int cacheDurationInSeconds) { cache.Set(key, value, TimeSpan.FromSeconds(cacheDurationInSeconds)); } public bool ContainsKey(string key) { return cache.Exists(key); } public V Get(string key) { return cache.Get(key).Value; } public IEnumerable GetAllKey() { return cache.GetByPrefix("SqlSugarDataCache.").Keys; } public void Remove(string key) { cache.Remove(key); } public V GetOrCreate(string cacheKey, Func create, int cacheDurationInSeconds = 2147483647) { if (cache.Exists(cacheKey)) { return cache.Get(cacheKey).Value; } V v = create(); cache.Set(cacheKey, v, TimeSpan.FromSeconds(cacheDurationInSeconds)); return v; } } }