SqlSugarCache.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using SqlSugar;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace WMS.BZSqlSugar
  8. {
  9. public class SqlSugarCache : ICacheService
  10. {
  11. public void Add<V>(string key, V value)
  12. {
  13. //RedisServer.Cache.Set(key, value, 3600 + RedisHelper.RandomExpired(5, 30));
  14. CacheHelper.SetCache(key, value);
  15. }
  16. public void Add<V>(string key, V value, int cacheDurationInSeconds)
  17. {
  18. //RedisServer.Cache.Set(key, value, cacheDurationInSeconds);
  19. CacheHelper.SetCaches(key, value, cacheDurationInSeconds);
  20. }
  21. public bool ContainsKey<V>(string key)
  22. {
  23. //return RedisServer.Cache.Exists(key);
  24. return CacheHelper.Exists(key);
  25. }
  26. public V Get<V>(string key)
  27. {
  28. //return RedisServer.Cache.Get<V>(key);
  29. return (V)CacheHelper.Get(key);
  30. }
  31. public IEnumerable<string> GetAllKey<V>()
  32. {
  33. //return RedisServer.Cache.Keys("*");
  34. return CacheHelper.GetCacheKeys();
  35. }
  36. public V GetOrCreate<V>(string cacheKey, Func<V> create, int cacheDurationInSeconds = int.MaxValue)
  37. {
  38. if (ContainsKey<V>(cacheKey))
  39. {
  40. var result = Get<V>(cacheKey);
  41. if (result == null)
  42. {
  43. return create();
  44. }
  45. else
  46. {
  47. return result;
  48. }
  49. }
  50. else
  51. {
  52. var restul = create();
  53. Add(cacheKey, restul, cacheDurationInSeconds);
  54. return restul;
  55. }
  56. }
  57. public void Remove<V>(string key)
  58. {
  59. //RedisServer.Cache.Del(key);
  60. CacheHelper.Remove(key);
  61. }
  62. }
  63. }