Builders.cs 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. namespace Best.HTTP.Caching
  3. {
  4. /// <summary>
  5. /// A builder struct for constructing an instance of the HTTPCache class with optional configuration options and callbacks.
  6. /// </summary>
  7. public struct HTTPCacheBuilder
  8. {
  9. private HTTPCacheOptions _options;
  10. private OnBeforeBeginCacheDelegate _callback;
  11. /// <summary>
  12. /// Sets the configuration options for the HTTP cache.
  13. /// </summary>
  14. /// <param name="options">The <see cref="HTTPCacheOptions"/> containing cache configuration settings.</param>
  15. /// <returns>The current <see cref="HTTPCacheBuilder"/> instance for method chaining.</returns>
  16. public HTTPCacheBuilder WithOptions(HTTPCacheOptions options)
  17. {
  18. this._options = options;
  19. return this;
  20. }
  21. /// <summary>
  22. /// Sets the configuration options for the HTTP cache using an <see cref="HTTPCacheOptionsBuilder"/>.
  23. /// </summary>
  24. /// <param name="optionsBuilder">An <see cref="HTTPCacheOptionsBuilder"/> for building cache configuration settings.</param>
  25. /// <returns>The current <see cref="HTTPCacheBuilder"/> instance for method chaining.</returns>
  26. public HTTPCacheBuilder WithOptions(HTTPCacheOptionsBuilder optionsBuilder)
  27. {
  28. this._options = optionsBuilder.Build();
  29. return this;
  30. }
  31. /// <summary>
  32. /// Sets a callback delegate to be executed before caching of an entity begins.
  33. /// </summary>
  34. /// <param name="callback">The delegate to be executed before caching starts.</param>
  35. /// <returns>The current <see cref="HTTPCacheBuilder"/> instance for method chaining.</returns>
  36. public HTTPCacheBuilder WithBeforeBeginCacheCallback(OnBeforeBeginCacheDelegate callback)
  37. {
  38. this._callback = callback;
  39. return this;
  40. }
  41. /// <summary>
  42. /// Builds and returns an instance of the <see cref="HTTPCache"/> with the specified configuration options and callback delegate.
  43. /// </summary>
  44. /// <returns>An <see cref="HTTPCache"/> instance configured with the specified options and callback.</returns>
  45. public HTTPCache Build()
  46. => new HTTPCache(this._options) { OnBeforeBeginCache = this._callback };
  47. }
  48. /// <summary>
  49. /// A builder struct for constructing an instance of <see cref="HTTPCacheOptions"/> with optional configuration settings.
  50. /// </summary>
  51. public struct HTTPCacheOptionsBuilder
  52. {
  53. private HTTPCacheOptions _options;
  54. /// <summary>
  55. /// Sets the maximum cache size for the HTTP cache.
  56. /// </summary>
  57. /// <param name="maxCacheSize">The maximum size, in bytes, that the cache can reach.</param>
  58. /// <returns>The current <see cref="HTTPCacheOptionsBuilder"/> instance for method chaining.</returns>
  59. public HTTPCacheOptionsBuilder WithMaxCacheSize(ulong maxCacheSize)
  60. {
  61. this._options = this._options ?? new HTTPCacheOptions();
  62. this._options.MaxCacheSize = maxCacheSize;
  63. return this;
  64. }
  65. /// <summary>
  66. /// Sets the maximum duration for which cached entries will be retained.
  67. /// By default all entities (even stalled ones) are kept cached until they are evicted to make room for new, fresh ones.
  68. /// </summary>
  69. /// <param name="olderThan">The maximum age for cached entries to be retained.</param>
  70. /// <returns>The current <see cref="HTTPCacheOptionsBuilder"/> instance for method chaining.</returns>
  71. public HTTPCacheOptionsBuilder WithDeleteOlderThen(TimeSpan olderThan)
  72. {
  73. this._options = this._options ?? new HTTPCacheOptions();
  74. this._options.DeleteOlder = olderThan;
  75. return this;
  76. }
  77. /// <summary>
  78. /// Builds and returns an instance of <see cref="HTTPCacheOptions"/> with the specified configuration settings.
  79. /// </summary>
  80. /// <returns>An <see cref="HTTPCacheOptions"/> instance configured with the specified settings.</returns>
  81. public HTTPCacheOptions Build()
  82. => this._options ?? new HTTPCacheOptions();
  83. }
  84. }