using System;
namespace Best.HTTP.Caching
{
///
/// A builder struct for constructing an instance of the HTTPCache class with optional configuration options and callbacks.
///
public struct HTTPCacheBuilder
{
private HTTPCacheOptions _options;
private OnBeforeBeginCacheDelegate _callback;
///
/// Sets the configuration options for the HTTP cache.
///
/// The containing cache configuration settings.
/// The current instance for method chaining.
public HTTPCacheBuilder WithOptions(HTTPCacheOptions options)
{
this._options = options;
return this;
}
///
/// Sets the configuration options for the HTTP cache using an .
///
/// An for building cache configuration settings.
/// The current instance for method chaining.
public HTTPCacheBuilder WithOptions(HTTPCacheOptionsBuilder optionsBuilder)
{
this._options = optionsBuilder.Build();
return this;
}
///
/// Sets a callback delegate to be executed before caching of an entity begins.
///
/// The delegate to be executed before caching starts.
/// The current instance for method chaining.
public HTTPCacheBuilder WithBeforeBeginCacheCallback(OnBeforeBeginCacheDelegate callback)
{
this._callback = callback;
return this;
}
///
/// Builds and returns an instance of the with the specified configuration options and callback delegate.
///
/// An instance configured with the specified options and callback.
public HTTPCache Build()
=> new HTTPCache(this._options) { OnBeforeBeginCache = this._callback };
}
///
/// A builder struct for constructing an instance of with optional configuration settings.
///
public struct HTTPCacheOptionsBuilder
{
private HTTPCacheOptions _options;
///
/// Sets the maximum cache size for the HTTP cache.
///
/// The maximum size, in bytes, that the cache can reach.
/// The current instance for method chaining.
public HTTPCacheOptionsBuilder WithMaxCacheSize(ulong maxCacheSize)
{
this._options = this._options ?? new HTTPCacheOptions();
this._options.MaxCacheSize = maxCacheSize;
return this;
}
///
/// Sets the maximum duration for which cached entries will be retained.
/// By default all entities (even stalled ones) are kept cached until they are evicted to make room for new, fresh ones.
///
/// The maximum age for cached entries to be retained.
/// The current instance for method chaining.
public HTTPCacheOptionsBuilder WithDeleteOlderThen(TimeSpan olderThan)
{
this._options = this._options ?? new HTTPCacheOptions();
this._options.DeleteOlder = olderThan;
return this;
}
///
/// Builds and returns an instance of with the specified configuration settings.
///
/// An instance configured with the specified settings.
public HTTPCacheOptions Build()
=> this._options ?? new HTTPCacheOptions();
}
}