ICache.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. namespace Abp.Runtime.Caching
  5. {
  6. /// <summary>
  7. /// Defines a cache that can be store and get items by keys.
  8. /// </summary>
  9. public interface ICache : IDisposable
  10. {
  11. /// <summary>
  12. /// Unique name of the cache.
  13. /// </summary>
  14. string Name { get; }
  15. /// <summary>
  16. /// Default sliding expire time of cache items.
  17. /// Default value: 60 minutes (1 hour).
  18. /// Can be changed by configuration.
  19. /// </summary>
  20. TimeSpan DefaultSlidingExpireTime { get; set; }
  21. /// <summary>
  22. /// Default absolute expire time of cache items.
  23. /// Default value: null (not used).
  24. /// </summary>
  25. TimeSpan? DefaultAbsoluteExpireTime { get; set; }
  26. /// <summary>
  27. /// Gets an item from the cache.
  28. /// This method hides cache provider failures (and logs them),
  29. /// uses the factory method to get the object if cache provider fails.
  30. /// </summary>
  31. /// <param name="key">Key</param>
  32. /// <param name="factory">Factory method to create cache item if not exists</param>
  33. /// <returns>Cached item</returns>
  34. object Get(string key, Func<string, object> factory);
  35. /// <summary>
  36. /// Gets items from the cache.
  37. /// This method hides cache provider failures (and logs them),
  38. /// uses the factory method to get the object if cache provider fails.
  39. /// </summary>
  40. /// <param name="keys">Keys</param>
  41. /// <param name="factory">Factory method to create cache item if not exists</param>
  42. /// <returns>Cached item</returns>
  43. object[] Get(string[] keys, Func<string, object> factory);
  44. /// <summary>
  45. /// Gets an item from the cache.
  46. /// This method hides cache provider failures (and logs them),
  47. /// uses the factory method to get the object if cache provider fails.
  48. /// </summary>
  49. /// <param name="key">Key</param>
  50. /// <param name="factory">Factory method to create cache item if not exists</param>
  51. /// <returns>Cached item</returns>
  52. Task<object> GetAsync(string key, Func<string, Task<object>> factory);
  53. /// <summary>
  54. /// Gets items from the cache.
  55. /// This method hides cache provider failures (and logs them),
  56. /// uses the factory method to get the object if cache provider fails.
  57. /// </summary>
  58. /// <param name="keys">Keys</param>
  59. /// <param name="factory">Factory method to create cache item if not exists</param>
  60. /// <returns>Cached items</returns>
  61. Task<object[]> GetAsync(string[] keys, Func<string, Task<object>> factory);
  62. /// <summary>
  63. /// Gets an item from the cache or null if not found.
  64. /// </summary>
  65. /// <param name="key">Key</param>
  66. /// <returns>Cached item or null if not found</returns>
  67. object GetOrDefault(string key);
  68. /// <summary>
  69. /// Gets items from the cache. For every key that is not found, a null value is returned.
  70. /// </summary>
  71. /// <param name="keys">Keys</param>
  72. /// <returns>Cached items</returns>
  73. object[] GetOrDefault(string[] keys);
  74. /// <summary>
  75. /// Gets an item from the cache or null if not found.
  76. /// </summary>
  77. /// <param name="key">Key</param>
  78. /// <returns>Cached item or null if not found</returns>
  79. Task<object> GetOrDefaultAsync(string key);
  80. /// <summary>
  81. /// Gets items from the cache. For every key that is not found, a null value is returned.
  82. /// </summary>
  83. /// <param name="keys">Keys</param>
  84. /// <returns>Cached items</returns>
  85. Task<object[]> GetOrDefaultAsync(string[] keys);
  86. /// <summary>
  87. /// Saves/Overrides an item in the cache by a key.
  88. /// Use one of the expire times at most (<paramref name="slidingExpireTime"/> or <paramref name="absoluteExpireTime"/>).
  89. /// If none of them is specified, then
  90. /// <see cref="DefaultAbsoluteExpireTime"/> will be used if it's not null. Othewise, <see cref="DefaultSlidingExpireTime"/>
  91. /// will be used.
  92. /// </summary>
  93. /// <param name="key">Key</param>
  94. /// <param name="value">Value</param>
  95. /// <param name="slidingExpireTime">Sliding expire time</param>
  96. /// <param name="absoluteExpireTime">Absolute expire time</param>
  97. void Set(string key, object value, TimeSpan? slidingExpireTime = null, TimeSpan? absoluteExpireTime = null);
  98. /// <summary>
  99. /// Saves/Overrides items in the cache by the pairs.
  100. /// Use one of the expire times at most (<paramref name="slidingExpireTime"/> or <paramref name="absoluteExpireTime"/>).
  101. /// If none of them is specified, then
  102. /// <see cref="DefaultAbsoluteExpireTime"/> will be used if it's not null. Othewise, <see cref="DefaultSlidingExpireTime"/>
  103. /// will be used.
  104. /// </summary>
  105. /// <param name="pairs">Pairs</param>
  106. /// <param name="slidingExpireTime">Sliding expire time</param>
  107. /// <param name="absoluteExpireTime">Absolute expire time</param>
  108. void Set(KeyValuePair<string, object>[] pairs, TimeSpan? slidingExpireTime = null, TimeSpan? absoluteExpireTime = null);
  109. /// <summary>
  110. /// Saves/Overrides an item in the cache by a key.
  111. /// Use one of the expire times at most (<paramref name="slidingExpireTime"/> or <paramref name="absoluteExpireTime"/>).
  112. /// If none of them is specified, then
  113. /// <see cref="DefaultAbsoluteExpireTime"/> will be used if it's not null. Othewise, <see cref="DefaultSlidingExpireTime"/>
  114. /// will be used.
  115. /// </summary>
  116. /// <param name="key">Key</param>
  117. /// <param name="value">Value</param>
  118. /// <param name="slidingExpireTime">Sliding expire time</param>
  119. /// <param name="absoluteExpireTime">Absolute expire time</param>
  120. Task SetAsync(string key, object value, TimeSpan? slidingExpireTime = null, TimeSpan? absoluteExpireTime = null);
  121. /// <summary>
  122. /// Saves/Overrides items in the cache by the pairs.
  123. /// Use one of the expire times at most (<paramref name="slidingExpireTime"/> or <paramref name="absoluteExpireTime"/>).
  124. /// If none of them is specified, then
  125. /// <see cref="DefaultAbsoluteExpireTime"/> will be used if it's not null. Othewise, <see cref="DefaultSlidingExpireTime"/>
  126. /// will be used.
  127. /// </summary>
  128. /// <param name="pairs">Pairs</param>
  129. /// <param name="slidingExpireTime">Sliding expire time</param>
  130. /// <param name="absoluteExpireTime">Absolute expire time</param>
  131. Task SetAsync(KeyValuePair<string, object>[] pairs, TimeSpan? slidingExpireTime = null, TimeSpan? absoluteExpireTime = null);
  132. /// <summary>
  133. /// Removes a cache item by it's key (does nothing if given key does not exists in the cache).
  134. /// </summary>
  135. /// <param name="key">Key</param>
  136. void Remove(string key);
  137. /// <summary>
  138. /// Removes cache items by their keys.
  139. /// </summary>
  140. /// <param name="keys">Keys</param>
  141. void Remove(string[] keys);
  142. /// <summary>
  143. /// Removes a cache item by it's key (does nothing if given key does not exists in the cache).
  144. /// </summary>
  145. /// <param name="key">Key</param>
  146. Task RemoveAsync(string key);
  147. /// <summary>
  148. /// Removes cache items by their keys.
  149. /// </summary>
  150. /// <param name="keys">Keys</param>
  151. Task RemoveAsync(string[] keys);
  152. /// <summary>
  153. /// Clears all items in this cache.
  154. /// </summary>
  155. void Clear();
  156. /// <summary>
  157. /// Clears all items in this cache.
  158. /// </summary>
  159. Task ClearAsync();
  160. }
  161. }