|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Linq; |
| 8 | +using System.Threading; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using Microsoft.Extensions.Caching.Distributed; |
| 11 | + |
| 12 | +namespace Microsoft.Extensions.Caching.Hybrid; |
| 13 | + |
| 14 | +/// <summary> |
| 15 | +/// Provides multi-tier caching services building on <see cref="IDistributedCache"/> backends. |
| 16 | +/// </summary> |
| 17 | +public abstract class HybridCache |
| 18 | +{ |
| 19 | + /// <summary> |
| 20 | + /// Asynchronously gets the value associated with the key if it exists, or generates a new entry using the provided key and a value from the given factory if the key is not found. |
| 21 | + /// </summary> |
| 22 | + /// <typeparam name="TState">The type of additional state required by <paramref name="factory"/>.</typeparam> |
| 23 | + /// <typeparam name="T">The type of the data being considered.</typeparam> |
| 24 | + /// <param name="key">The key of the entry to look for or create.</param> |
| 25 | + /// <param name="factory">Provides the underlying data service is the data is not available in the cache.</param> |
| 26 | + /// <param name="state">The state required for <paramref name="factory"/>.</param> |
| 27 | + /// <param name="options">Additional options for this cache entry.</param> |
| 28 | + /// <param name="tags">The tags to associate with this cache item.</param> |
| 29 | + /// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param> |
| 30 | + /// <returns>The data, either from cache or the underlying data service.</returns> |
| 31 | + public abstract ValueTask<T> GetOrCreateAsync<TState, T>(string key, TState state, Func<TState, CancellationToken, ValueTask<T>> factory, |
| 32 | + HybridCacheEntryOptions? options = null, IEnumerable<string>? tags = null, CancellationToken cancellationToken = default); |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Asynchronously gets the value associated with the key if it exists, or generates a new entry using the provided key and a value from the given factory if the key is not found. |
| 36 | + /// </summary> |
| 37 | + /// <typeparam name="T">The type of the data being considered.</typeparam> |
| 38 | + /// <param name="key">The key of the entry to look for or create.</param> |
| 39 | + /// <param name="factory">Provides the underlying data service is the data is not available in the cache.</param> |
| 40 | + /// <param name="options">Additional options for this cache entry.</param> |
| 41 | + /// <param name="tags">The tags to associate with this cache item.</param> |
| 42 | + /// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param> |
| 43 | + /// <returns>The data, either from cache or the underlying data service.</returns> |
| 44 | + public ValueTask<T> GetOrCreateAsync<T>(string key, Func<CancellationToken, ValueTask<T>> factory, |
| 45 | + HybridCacheEntryOptions? options = null, IEnumerable<string>? tags = null, CancellationToken cancellationToken = default) |
| 46 | + => GetOrCreateAsync(key, factory, WrappedCallbackCache<T>.Instance, options, tags, cancellationToken); |
| 47 | + |
| 48 | + private static class WrappedCallbackCache<T> // per-T memoized helper that allows GetOrCreateAsync<T> and GetOrCreateAsync<TState, T> to share an implementation |
| 49 | + { |
| 50 | + // for the simple usage scenario (no TState), pack the original callback as the "state", and use a wrapper function that just unrolls and invokes from the state |
| 51 | + public static readonly Func<Func<CancellationToken, ValueTask<T>>, CancellationToken, ValueTask<T>> Instance = static (callback, ct) => callback(ct); |
| 52 | + } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Asynchronously sets or overwrites the value associated with the key. |
| 56 | + /// </summary> |
| 57 | + /// <typeparam name="T">The type of the data being considered.</typeparam> |
| 58 | + /// <param name="key">The key of the entry to create.</param> |
| 59 | + /// <param name="value">The value to assign for this cache entry.</param> |
| 60 | + /// <param name="options">Additional options for this cache entry.</param> |
| 61 | + /// <param name="tags">The tags to associate with this cache entry.</param> |
| 62 | + /// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param> |
| 63 | + public abstract ValueTask SetAsync<T>(string key, T value, HybridCacheEntryOptions? options = null, IEnumerable<string>? tags = null, CancellationToken cancellationToken = default); |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Asynchronously removes the value associated with the key if it exists. |
| 67 | + /// </summary> |
| 68 | + public abstract ValueTask RemoveAsync(string key, CancellationToken cancellationToken = default); |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Asynchronously removes the value associated with the key if it exists. |
| 72 | + /// </summary> |
| 73 | + /// <remarks>Implementors should treat <c>null</c> as empty</remarks> |
| 74 | + public virtual ValueTask RemoveAsync(IEnumerable<string> keys, CancellationToken cancellationToken = default) |
| 75 | + { |
| 76 | + return keys switch |
| 77 | + { |
| 78 | + // for consistency with GetOrCreate/Set: interpret null as "none" |
| 79 | + null or ICollection<string> { Count: 0 } => default, |
| 80 | + ICollection<string> { Count: 1 } => RemoveAsync(keys.First(), cancellationToken), |
| 81 | + _ => ForEachAsync(this, keys, cancellationToken), |
| 82 | + }; |
| 83 | + |
| 84 | + // default implementation is to call RemoveAsync for each key in turn |
| 85 | + static async ValueTask ForEachAsync(HybridCache @this, IEnumerable<string> keys, CancellationToken cancellationToken) |
| 86 | + { |
| 87 | + foreach (var key in keys) |
| 88 | + { |
| 89 | + await @this.RemoveAsync(key, cancellationToken).ConfigureAwait(false); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// Asynchronously removes all values associated with the specified tags. |
| 96 | + /// </summary> |
| 97 | + /// <remarks>Implementors should treat <c>null</c> as empty</remarks> |
| 98 | + public virtual ValueTask RemoveByTagAsync(IEnumerable<string> tags, CancellationToken cancellationToken = default) |
| 99 | + { |
| 100 | + return tags switch |
| 101 | + { |
| 102 | + // for consistency with GetOrCreate/Set: interpret null as "none" |
| 103 | + null or ICollection<string> { Count: 0 } => default, |
| 104 | + ICollection<string> { Count: 1 } => RemoveByTagAsync(tags.Single(), cancellationToken), |
| 105 | + _ => ForEachAsync(this, tags, cancellationToken), |
| 106 | + }; |
| 107 | + |
| 108 | + // default implementation is to call RemoveByTagAsync for each key in turn |
| 109 | + static async ValueTask ForEachAsync(HybridCache @this, IEnumerable<string> keys, CancellationToken cancellationToken) |
| 110 | + { |
| 111 | + foreach (var key in keys) |
| 112 | + { |
| 113 | + await @this.RemoveByTagAsync(key, cancellationToken).ConfigureAwait(false); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + /// <summary> |
| 119 | + /// Asynchronously removes all values associated with the specified tag. |
| 120 | + /// </summary> |
| 121 | + public abstract ValueTask RemoveByTagAsync(string tag, CancellationToken cancellationToken = default); |
| 122 | +} |
0 commit comments