ITypedCache.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace XF.Domain.Cache
  7. {
  8. public interface ITypedCache<TValue> //where TValue : class
  9. {
  10. /*
  11. TrySet/TrySetAsync
  12. Set/SetAsync
  13. SetAll/SetAllAsync
  14. Get/GetAsync(with data retriever)
  15. Get/GetAsync(without data retriever)
  16. GetByPrefix/GetByPrefixAsync
  17. GetAll/GetAllAsync
  18. Remove/RemoveAsync
  19. RemoveByPrefix/RemoveByPrefixAsync
  20. RemoveAll/RemoveAllAsync
  21. Flush/FlushAsync
  22. GetCount
  23. GetExpiration/GetExpirationAsync
  24. */
  25. void Set(string key, TValue value, TimeSpan? expiration = null);
  26. Task SetAsync(string key, TValue value, TimeSpan? expiration = null, CancellationToken cancellationToken = default);
  27. TValue? Get(string key);
  28. Task<TValue?> GetAsync(string key, CancellationToken cancellationToken);
  29. void Remove(string key);
  30. Task RemoveAsync(string key, CancellationToken cancellationToken);
  31. bool Exists(string key);
  32. Task<bool> ExistsAsync(string key, CancellationToken cancellationToken);
  33. bool TrySet(string key, TValue value, TimeSpan? expiration = null);
  34. Task<bool> TrySetAsync(string key, TValue value, TimeSpan? expiration = null, CancellationToken cancellationToken = default);
  35. void SetAll(IDictionary<string, TValue> values, TimeSpan? expiration = null);
  36. Task SetAllAsync(IDictionary<string, TValue> values, TimeSpan? expiration = null, CancellationToken cancellationToken = default);
  37. IDictionary<string, TValue> GetByPrefix();
  38. Task<IDictionary<string, TValue>> GetByPrefixAsync(CancellationToken cancellationToken);
  39. IReadOnlyList<TValue> GetListByPrefix();
  40. Task<IReadOnlyList<TValue>> GetListByPrefixAsync(CancellationToken cancellationToken);
  41. //void RemoveByPrefix(string prefix);
  42. //Task RemoveByPrefixAsync(string prefix, CancellationToken cancellationToken);
  43. /// <summary>
  44. /// get the cache value, if cache not exists, set the value to cache then return
  45. /// </summary>
  46. TValue GetOrSet(string key, TValue value, TimeSpan? expiration = null);
  47. TValue? GetOrSet(string key, Func<string, TValue> valueFactory, TimeSpan? expiration = null);
  48. Task<TValue> GetOrSetAsync(string key, TValue value, TimeSpan? expiration = null, CancellationToken cancellationToken = default);
  49. Task<TValue?> GetOrSetAsync(string key, Func<string, TValue> valueFactory, TimeSpan? expiration = null, CancellationToken cancellationToken = default);
  50. //TValue GetOrAdd(string key, TValue value, ExpireMode? expireMode = ExpireMode.None, TimeSpan? timeout = null);
  51. //TValue GetOrAdd(string key, Func<string, TValue> valueFactory, ExpireMode? expireMode = ExpireMode.None, TimeSpan? timeout = null);
  52. string CombinePrefix(string key);
  53. }
  54. public enum ExpireMode
  55. {
  56. /// <summary>Defines no expiration.</summary>
  57. None,
  58. /// <summary>
  59. /// Defines sliding expiration. The expiration timeout will be refreshed on every access.
  60. /// </summary>
  61. Sliding,
  62. /// <summary>
  63. /// Defines absolute expiration. The item will expire after the expiration timeout.
  64. /// </summary>
  65. Absolute,
  66. }
  67. }