CacheExtensions.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. namespace Abp.Runtime.Caching
  5. {
  6. /// <summary>
  7. /// Extension methods for <see cref="ICache"/>.
  8. /// </summary>
  9. public static class CacheExtensions
  10. {
  11. public static object Get(this ICache cache, string key, Func<object> factory)
  12. {
  13. return cache.Get(key, k => factory());
  14. }
  15. public static object[] Get(this ICache cache, string[] keys, Func<object> factory)
  16. {
  17. return keys.Select(key => cache.Get(key, k => factory())).ToArray();
  18. }
  19. public static Task<object> GetAsync(this ICache cache, string key, Func<Task<object>> factory)
  20. {
  21. return cache.GetAsync(key, k => factory());
  22. }
  23. public static Task<object[]> GetAsync(this ICache cache, string[] keys, Func<Task<object>> factory)
  24. {
  25. var tasks = keys.Select(key => cache.GetAsync(key, k => factory()));
  26. return Task.WhenAll(tasks);
  27. }
  28. public static ITypedCache<TKey, TValue> AsTyped<TKey, TValue>(this ICache cache)
  29. {
  30. return new TypedCacheWrapper<TKey, TValue>(cache);
  31. }
  32. public static TValue Get<TKey, TValue>(this ICache cache, TKey key, Func<TKey, TValue> factory)
  33. {
  34. return (TValue)cache.Get(key.ToString(), (k) => (object)factory(key));
  35. }
  36. public static TValue[] Get<TKey, TValue>(this ICache cache, TKey[] keys, Func<TKey, TValue> factory)
  37. {
  38. return keys.Select(key => (TValue)cache.Get(key.ToString(), (k) => (object)factory(key))).ToArray();
  39. }
  40. public static TValue Get<TKey, TValue>(this ICache cache, TKey key, Func<TValue> factory)
  41. {
  42. return cache.Get(key, (k) => factory());
  43. }
  44. public static TValue[] Get<TKey, TValue>(this ICache cache, TKey[] keys, Func<TValue> factory)
  45. {
  46. return keys.Select(key => cache.Get(key, (k) => factory())).ToArray();
  47. }
  48. public static async Task<TValue> GetAsync<TKey, TValue>(this ICache cache, TKey key, Func<TKey, Task<TValue>> factory)
  49. {
  50. var value = await cache.GetAsync(key.ToString(), async (keyAsString) =>
  51. {
  52. var v = await factory(key);
  53. return (object)v;
  54. });
  55. return (TValue)value;
  56. }
  57. public static async Task<TValue[]> GetAsync<TKey, TValue>(this ICache cache, TKey[] keys, Func<TKey, Task<TValue>> factory)
  58. {
  59. var tasks = keys.Select(key =>
  60. {
  61. return cache.GetAsync(key.ToString(), async (keyAsString) =>
  62. {
  63. var v = await factory(key);
  64. return (object)v;
  65. });
  66. });
  67. var values = await Task.WhenAll(tasks);
  68. return values.Select(value => (TValue)value).ToArray();
  69. }
  70. public static Task<TValue> GetAsync<TKey, TValue>(this ICache cache, TKey key, Func<Task<TValue>> factory)
  71. {
  72. return cache.GetAsync(key, (k) => factory());
  73. }
  74. public static Task<TValue[]> GetAsync<TKey, TValue>(this ICache cache, TKey[] keys, Func<Task<TValue>> factory)
  75. {
  76. var tasks = keys.Select(key => cache.GetAsync(key, (k) => factory()));
  77. return Task.WhenAll(tasks);
  78. }
  79. public static TValue GetOrDefault<TKey, TValue>(this ICache cache, TKey key)
  80. {
  81. var value = cache.GetOrDefault(key.ToString());
  82. if (value == null)
  83. {
  84. return default(TValue);
  85. }
  86. return (TValue)value;
  87. }
  88. public static TValue[] GetOrDefault<TKey, TValue>(this ICache cache, TKey[] keys)
  89. {
  90. var values = keys.Select(key =>
  91. {
  92. var value = cache.GetOrDefault(key.ToString());
  93. if (value == null)
  94. {
  95. return default(TValue);
  96. }
  97. return (TValue)value;
  98. });
  99. return values.ToArray();
  100. }
  101. public static async Task<TValue> GetOrDefaultAsync<TKey, TValue>(this ICache cache, TKey key)
  102. {
  103. var value = await cache.GetOrDefaultAsync(key.ToString());
  104. if (value == null)
  105. {
  106. return default(TValue);
  107. }
  108. return (TValue)value;
  109. }
  110. public static async Task<TValue[]> GetOrDefaultAsync<TKey, TValue>(this ICache cache, TKey[] keys)
  111. {
  112. var tasks = keys.Select(async (key) =>
  113. {
  114. var value = await cache.GetOrDefaultAsync(key.ToString());
  115. if (value == null)
  116. {
  117. return default(TValue);
  118. }
  119. return (TValue)value;
  120. });
  121. return await Task.WhenAll(tasks);
  122. }
  123. }
  124. }