SqlSugarRedisCache.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using ZR.Common.Cache;
  2. namespace ZR.ServiceCore.SqlSugar
  3. {
  4. /// <summary>
  5. /// 数据库Redis缓存
  6. /// </summary>
  7. public class SqlSugarRedisCache : ICacheService
  8. {
  9. public void Add<V>(string key, V value)
  10. {
  11. RedisServer.Cache.Set(key, value);
  12. }
  13. public void Add<V>(string key, V value, int cacheDurationInSeconds)
  14. {
  15. RedisServer.Cache.Set(key, value, cacheDurationInSeconds);
  16. }
  17. public bool ContainsKey<V>(string key)
  18. {
  19. return RedisServer.Cache.Exists(key);
  20. }
  21. public V Get<V>(string key)
  22. {
  23. return RedisServer.Cache.Get<V>(key);
  24. }
  25. public IEnumerable<string> GetAllKey<V>()
  26. {
  27. return RedisServer.Cache.Keys("SqlSugarDataCache.*");
  28. }
  29. public V GetOrCreate<V>(string cacheKey, Func<V> create, int cacheDurationInSeconds = int.MaxValue)
  30. {
  31. if (ContainsKey<V>(cacheKey))
  32. {
  33. var result = Get<V>(cacheKey);
  34. if (result == null)
  35. {
  36. return create();
  37. }
  38. else
  39. {
  40. return result;
  41. }
  42. }
  43. else
  44. {
  45. var restul = create();
  46. Add(cacheKey, restul, cacheDurationInSeconds);
  47. return restul;
  48. }
  49. }
  50. public void Remove<V>(string key)
  51. {
  52. RedisServer.Cache.Del(key);
  53. }
  54. }
  55. }