1
0

RedisCache.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. 
  2. using System;
  3. using System.Text.RegularExpressions;
  4. using System.Threading.Tasks;
  5. namespace Admin.Core.Common.Cache
  6. {
  7. /// <summary>
  8. /// Redis缓存
  9. /// </summary>
  10. public class RedisCache : ICache
  11. {
  12. public long Del(params string[] key)
  13. {
  14. return RedisHelper.Del(key);
  15. }
  16. public Task<long> DelAsync(params string[] key)
  17. {
  18. return RedisHelper.DelAsync(key);
  19. }
  20. public async Task<long> DelByPatternAsync(string pattern)
  21. {
  22. if (pattern.IsNull())
  23. return default;
  24. pattern = Regex.Replace(pattern, @"\{.*\}", "*");
  25. var keys = (await RedisHelper.KeysAsync(pattern));
  26. if(keys != null && keys.Length > 0)
  27. {
  28. return await RedisHelper.DelAsync(keys);
  29. }
  30. return default;
  31. }
  32. public bool Exists(string key)
  33. {
  34. return RedisHelper.Exists(key);
  35. }
  36. public Task<bool> ExistsAsync(string key)
  37. {
  38. return RedisHelper.ExistsAsync(key);
  39. }
  40. public string Get(string key)
  41. {
  42. return RedisHelper.Get(key);
  43. }
  44. public T Get<T>(string key)
  45. {
  46. return RedisHelper.Get<T>(key);
  47. }
  48. public Task<string> GetAsync(string key)
  49. {
  50. return RedisHelper.GetAsync(key);
  51. }
  52. public Task<T> GetAsync<T>(string key)
  53. {
  54. return RedisHelper.GetAsync<T>(key);
  55. }
  56. public bool Set(string key, object value)
  57. {
  58. return RedisHelper.Set(key, value);
  59. }
  60. public bool Set(string key, object value, TimeSpan expire)
  61. {
  62. return RedisHelper.Set(key, value, expire);
  63. }
  64. public Task<bool> SetAsync(string key, object value)
  65. {
  66. return RedisHelper.SetAsync(key, value);
  67. }
  68. public Task<bool> SetAsync(string key, object value, TimeSpan expire)
  69. {
  70. return RedisHelper.SetAsync(key, value, expire);
  71. }
  72. }
  73. }