MapperCache.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace SqlSugar
  6. {
  7. public class MapperCache<T>
  8. {
  9. private Dictionary<string, object> caches = new Dictionary<string, object>();
  10. private List<T> _list { get; set; }
  11. private ISqlSugarClient _context { get; set; }
  12. public int GetIndex { get; set; }
  13. private MapperCache()
  14. {
  15. }
  16. public MapperCache(List<T> list, ISqlSugarClient context)
  17. {
  18. _list = list;
  19. _context = context;
  20. }
  21. public Result Get<Result>(Func<List<T>, Result> action)
  22. {
  23. GetIndex++;
  24. string key = "Get" +typeof(Result)+action.GetHashCode()+action.Method.Name;
  25. if (caches.ContainsKey(key))
  26. {
  27. return (Result)caches[key];
  28. }
  29. else
  30. {
  31. var result = action(_list);
  32. caches.Add(key, result);
  33. return result;
  34. }
  35. }
  36. public Result Get<Result>(Func<List<T>, Result> action,string cachekey)
  37. {
  38. GetIndex++;
  39. string key = "Get" + typeof(Result) + action.GetHashCode() + action.Method.Name+ cachekey;
  40. if (caches.ContainsKey(key))
  41. {
  42. return (Result)caches[key];
  43. }
  44. else
  45. {
  46. var result = action(_list);
  47. caches.Add(key, result);
  48. return result;
  49. }
  50. }
  51. public List<Result> GetListByPrimaryKeys<Result>(Func<T, double?> action) where Result : class, new()
  52. {
  53. {
  54. string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString();
  55. return GetListByPrimaryKeys<Result, double?>(action, key);
  56. }
  57. }
  58. public List<Result> GetListByPrimaryKeys<Result>(Func<T, double> action) where Result : class, new()
  59. {
  60. {
  61. string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString();
  62. return GetListByPrimaryKeys<Result, double>(action, key);
  63. }
  64. }
  65. public List<Result> GetListByPrimaryKeys<Result>(Func<T, decimal?> action) where Result : class, new()
  66. {
  67. {
  68. string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString();
  69. return GetListByPrimaryKeys<Result, decimal?>(action, key);
  70. }
  71. }
  72. public List<Result> GetListByPrimaryKeys<Result>(Func<T, decimal> action) where Result : class, new()
  73. {
  74. {
  75. string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString();
  76. return GetListByPrimaryKeys<Result, decimal>(action, key);
  77. }
  78. }
  79. public List<Result> GetListByPrimaryKeys<Result>(Func<T, int?> action) where Result : class, new()
  80. {
  81. {
  82. string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString();
  83. return GetListByPrimaryKeys<Result,int?>(action, key);
  84. }
  85. }
  86. public List<Result> GetListByPrimaryKeys<Result>(Func<T, int> action) where Result : class, new()
  87. {
  88. {
  89. string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString();
  90. return GetListByPrimaryKeys<Result, int>(action, key);
  91. }
  92. }
  93. public List<Result> GetListByPrimaryKeys<Result>(Func<T, long?> action) where Result : class, new()
  94. {
  95. {
  96. string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString();
  97. return GetListByPrimaryKeys<Result, long?>(action, key);
  98. }
  99. }
  100. public List<Result> GetListByPrimaryKeys<Result>(Func<T, long> action) where Result : class, new()
  101. {
  102. {
  103. string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString();
  104. return GetListByPrimaryKeys<Result, long>(action, key);
  105. }
  106. }
  107. public List<Result> GetListByPrimaryKeys<Result>(Func<T, string> action) where Result : class, new()
  108. {
  109. {
  110. string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString();
  111. return GetListByPrimaryKeys<Result, string>(action, key);
  112. }
  113. }
  114. public List<Result> GetListByPrimaryKeys<Result>(Func<T, Guid> action) where Result : class, new()
  115. {
  116. {
  117. string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString();
  118. return GetListByPrimaryKeys<Result, Guid>(action, key);
  119. }
  120. }
  121. public List<Result> GetListByPrimaryKeys<Result>(Func<T, Guid?> action) where Result : class, new()
  122. {
  123. {
  124. string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString();
  125. return GetListByPrimaryKeys<Result, Guid?>(action, key);
  126. }
  127. }
  128. public List<Result> GetListByPrimaryKeys<Result>(Func<T, DateTime> action) where Result : class, new()
  129. {
  130. {
  131. string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString();
  132. return GetListByPrimaryKeys<Result, DateTime>(action, key);
  133. }
  134. }
  135. public List<Result> GetListByPrimaryKeys<Result>(Func<T, DateTime?> action) where Result : class, new()
  136. {
  137. {
  138. string key = "GetListById" + typeof(Result) + action.GetHashCode().ToString();
  139. return GetListByPrimaryKeys<Result, DateTime?>(action, key);
  140. }
  141. }
  142. private List<Result> GetListByPrimaryKeys<Result,FieldType>(Func<T, FieldType> action, string key) where Result : class, new()
  143. {
  144. if (caches.ContainsKey(key))
  145. {
  146. return (List<Result>)caches[key];
  147. }
  148. else
  149. {
  150. var ids = _list.Select(action).ToList().Distinct().ToList();
  151. var result = _context.Queryable<Result>().In(ids).ToList();
  152. caches.Add(key, result);
  153. return result;
  154. }
  155. }
  156. }
  157. }