SqlSugarCache.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. namespace Admin.NET.Core;
  2. /// <summary>
  3. /// SqlSugar二级缓存
  4. /// </summary>
  5. public class SqlSugarCache : ICacheService
  6. {
  7. private static readonly ICache _cache = App.GetService(typeof(ICache)) as ICache;
  8. public void Add<V>(string key, V value)
  9. {
  10. _cache.Set(key, value);
  11. }
  12. public void Add<V>(string key, V value, int cacheDurationInSeconds)
  13. {
  14. _cache.Set(key, value, cacheDurationInSeconds);
  15. }
  16. public bool ContainsKey<V>(string key)
  17. {
  18. return _cache.ContainsKey(key);
  19. }
  20. public V Get<V>(string key)
  21. {
  22. return _cache.Get<V>(key);
  23. }
  24. public IEnumerable<string> GetAllKey<V>()
  25. {
  26. return _cache.Keys;
  27. }
  28. public V GetOrCreate<V>(string cacheKey, Func<V> create, int cacheDurationInSeconds = int.MaxValue)
  29. {
  30. if (!_cache.TryGetValue<V>(cacheKey, out V value))
  31. {
  32. value = create();
  33. _cache.Set(cacheKey, value, cacheDurationInSeconds);
  34. }
  35. return value;
  36. }
  37. public void Remove<V>(string key)
  38. {
  39. _cache.Remove(key);
  40. }
  41. }