SqlSugarCache.cs 1.8 KB

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