Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
New and improved C# IDistributedCacheInterface
/// <summary>
/// Simplified and improved IDistributedCache interface. No more stupid byte arrays. Removal events
/// can also be hooked into allowing removal from a local memory cache if desired.
/// </summary>
public interface IDistributedCache
{
/// <summary>
/// Fires when an item is removed by another machine, parameters are the key and the machine name
/// </summary>
event Action<DistributedCacheRemoveEventArgs>? Removed;
/// <summary>
/// Get a cache value, external caches (if any) are searched
/// </summary>
/// <typeparam name="T">Type of value</typeparam>
/// <param name="key">Key</param>
/// <param name="cancelToken">Cancel token</param>
/// <returns>Task of type T, T will be null if not found</returns>
ValueTask<T?> GetAsync<T>(string key, CancellationToken cancelToken = default) where T : class;
/// <summary>
/// Set a cache value, overwriting any existing value
/// </summary>
/// <typeparam name="T">Type of object to set</typeparam>
/// <param name="key">Key</param>
/// <param name="value">Value</param>
/// <param name="cacheParameters">Cache parameters or null for default</param>
/// <param name="cancelToken">Cancel token</param>
/// <returns>Task</returns>
ValueTask SetAsync<T>(string key, T value, CacheParameters? cacheParameters = null, CancellationToken cancelToken = default) where T : class;
/// <summary>
/// Remove an item from the cache
/// </summary>
/// <param name="key">Key</param>
/// <param name="cancelToken">Cancel token</param>
/// <returns>Task</returns>
ValueTask RemoveAsync(string key, CancellationToken cancelToken = default);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment