CacheSchemeMain.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace SqlSugar
  6. {
  7. internal class CacheSchemeMain
  8. {
  9. public static T GetOrCreate<T>(ICacheService cacheService, QueryBuilder queryBuilder, Func<T> getData, int cacheDurationInSeconds, SqlSugarProvider context,string cacheKey)
  10. {
  11. CacheKey key = CacheKeyBuider.GetKey(context, queryBuilder);
  12. key.AppendKey = cacheKey;
  13. string keyString = key.ToString();
  14. var result = cacheService.GetOrCreate(keyString, getData, cacheDurationInSeconds);
  15. return result;
  16. }
  17. public static void RemoveCache(ICacheService cacheService, string tableName)
  18. {
  19. if (cacheService == null)
  20. {
  21. return;
  22. }
  23. if (StaticConfig.CacheRemoveByLikeStringFunc != null)
  24. {
  25. StaticConfig.CacheRemoveByLikeStringFunc(cacheService, UtilConstants.Dot + tableName.ToLower() + UtilConstants.Dot);
  26. return;
  27. }
  28. var keys = cacheService.GetAllKey<string>();
  29. if (keys.HasValue())
  30. {
  31. foreach (var item in keys)
  32. {
  33. if (item.ToLower().Contains(UtilConstants.Dot + tableName.ToLower() + UtilConstants.Dot))
  34. {
  35. cacheService.Remove<string>(item);
  36. }
  37. }
  38. }
  39. }
  40. public static void RemoveCacheByLike(ICacheService cacheService, string likeString)
  41. {
  42. if (cacheService == null)
  43. {
  44. return;
  45. }
  46. if (StaticConfig.CacheRemoveByLikeStringFunc != null)
  47. {
  48. StaticConfig.CacheRemoveByLikeStringFunc(cacheService, likeString);
  49. return;
  50. }
  51. var keys = cacheService.GetAllKey<string>();
  52. if (keys.HasValue())
  53. {
  54. foreach (var item in keys)
  55. {
  56. if (item.ToLower().Contains(likeString.ToLower()))
  57. {
  58. cacheService.Remove<string>(item);
  59. }
  60. }
  61. }
  62. }
  63. }
  64. }