|
5 | 5 | using System.Collections; |
6 | 6 | using System.Collections.Generic; |
7 | 7 | using System.Linq; |
| 8 | +using System.Runtime.CompilerServices; |
8 | 9 | using System.Threading; |
9 | 10 | using System.Threading.Tasks; |
10 | 11 | using Microsoft.Extensions.Caching.Distributed; |
@@ -45,6 +46,101 @@ public ValueTask<T> GetOrCreateAsync<T>(string key, Func<CancellationToken, Valu |
45 | 46 | HybridCacheEntryOptions? options = null, IEnumerable<string>? tags = null, CancellationToken cancellationToken = default) |
46 | 47 | => GetOrCreateAsync(key, factory, WrappedCallbackCache<T>.Instance, options, tags, cancellationToken); |
47 | 48 |
|
| 49 | +#if NET10_0_OR_GREATER |
| 50 | + /// <summary> |
| 51 | + /// 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. |
| 52 | + /// </summary> |
| 53 | + /// <typeparam name="T">The type of the data being considered.</typeparam> |
| 54 | + /// <param name="key">The key of the entry to look for or create.</param> |
| 55 | + /// <param name="factory">Provides the underlying data service if the data is not available in the cache.</param> |
| 56 | + /// <param name="options">Additional options for this cache entry.</param> |
| 57 | + /// <param name="tags">The tags to associate with this cache item.</param> |
| 58 | + /// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param> |
| 59 | + /// <returns>The data, either from cache or the underlying data service.</returns> |
| 60 | + public ValueTask<T> GetOrCreateAsync<T>( |
| 61 | + ReadOnlySpan<char> key, |
| 62 | + Func<CancellationToken, ValueTask<T>> factory, |
| 63 | + HybridCacheEntryOptions? options = null, |
| 64 | + IEnumerable<string>? tags = null, |
| 65 | + CancellationToken cancellationToken = default) |
| 66 | + => GetOrCreateAsync(key, factory, WrappedCallbackCache<T>.Instance, options, tags, cancellationToken); |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// 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. |
| 70 | + /// </summary> |
| 71 | + /// <typeparam name="TState">The type of additional state required by <paramref name="factory"/>.</typeparam> |
| 72 | + /// <typeparam name="T">The type of the data being considered.</typeparam> |
| 73 | + /// <param name="key">The key of the entry to look for or create.</param> |
| 74 | + /// <param name="factory">Provides the underlying data service if the data is not available in the cache.</param> |
| 75 | + /// <param name="state">The state required for <paramref name="factory"/>.</param> |
| 76 | + /// <param name="options">Additional options for this cache entry.</param> |
| 77 | + /// <param name="tags">The tags to associate with this cache item.</param> |
| 78 | + /// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param> |
| 79 | + /// <returns>The data, either from cache or the underlying data service.</returns> |
| 80 | + /// <remarks>Implementors may use the key span to attempt a local-cache synchronous 'get' without requiring the key as a <see cref="string"/>.</remarks> |
| 81 | + public virtual ValueTask<T> GetOrCreateAsync<TState, T>( |
| 82 | + ReadOnlySpan<char> key, |
| 83 | + TState state, |
| 84 | + Func<TState, CancellationToken, ValueTask<T>> factory, |
| 85 | + HybridCacheEntryOptions? options = null, |
| 86 | + IEnumerable<string>? tags = null, |
| 87 | + CancellationToken cancellationToken = default) |
| 88 | + => GetOrCreateAsync(key.ToString(), state, factory, options, tags, cancellationToken); |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// 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. |
| 92 | + /// </summary> |
| 93 | + /// <typeparam name="T">The type of the data being considered.</typeparam> |
| 94 | + /// <param name="key">The key of the entry to look for or create.</param> |
| 95 | + /// <param name="factory">Provides the underlying data service if the data is not available in the cache.</param> |
| 96 | + /// <param name="options">Additional options for this cache entry.</param> |
| 97 | + /// <param name="tags">The tags to associate with this cache item.</param> |
| 98 | + /// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param> |
| 99 | + /// <returns>The data, either from cache or the underlying data service.</returns> |
| 100 | + public ValueTask<T> GetOrCreateAsync<T>( |
| 101 | + ref DefaultInterpolatedStringHandler key, |
| 102 | + Func<CancellationToken, ValueTask<T>> factory, |
| 103 | + HybridCacheEntryOptions? options = null, |
| 104 | + IEnumerable<string>? tags = null, |
| 105 | + CancellationToken cancellationToken = default) |
| 106 | + { |
| 107 | + // It is *not* an error that this Clear occurs before the "await"; by definition, the implementation is *required* to copy |
| 108 | + // the value locally before an await, precisely because the ref-struct cannot bridge an await. Thus: we are fine to clean |
| 109 | + // the buffer even in the non-synchronous completion scenario. |
| 110 | + ValueTask<T> result = GetOrCreateAsync(key.Text, factory, WrappedCallbackCache<T>.Instance, options, tags, cancellationToken); |
| 111 | + key.Clear(); |
| 112 | + return result; |
| 113 | + } |
| 114 | + |
| 115 | + /// <summary> |
| 116 | + /// 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. |
| 117 | + /// </summary> |
| 118 | + /// <typeparam name="TState">The type of additional state required by <paramref name="factory"/>.</typeparam> |
| 119 | + /// <typeparam name="T">The type of the data being considered.</typeparam> |
| 120 | + /// <param name="key">The key of the entry to look for or create.</param> |
| 121 | + /// <param name="factory">Provides the underlying data service if the data is not available in the cache.</param> |
| 122 | + /// <param name="state">The state required for <paramref name="factory"/>.</param> |
| 123 | + /// <param name="options">Additional options for this cache entry.</param> |
| 124 | + /// <param name="tags">The tags to associate with this cache item.</param> |
| 125 | + /// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param> |
| 126 | + /// <returns>The data, either from cache or the underlying data service.</returns> |
| 127 | + public ValueTask<T> GetOrCreateAsync<TState, T>( |
| 128 | + ref DefaultInterpolatedStringHandler key, |
| 129 | + TState state, |
| 130 | + Func<TState, CancellationToken, ValueTask<T>> factory, |
| 131 | + HybridCacheEntryOptions? options = null, |
| 132 | + IEnumerable<string>? tags = null, |
| 133 | + CancellationToken cancellationToken = default) |
| 134 | + { |
| 135 | + // It is *not* an error that this Clear occurs before the "await"; by definition, the implementation is *required* to copy |
| 136 | + // the value locally before an await, precisely because the ref-struct cannot bridge an await. Thus: we are fine to clean |
| 137 | + // the buffer even in the non-synchronous completion scenario. |
| 138 | + ValueTask<T> result = GetOrCreateAsync(key.Text, state, factory, options, tags, cancellationToken); |
| 139 | + key.Clear(); |
| 140 | + return result; |
| 141 | + } |
| 142 | +#endif |
| 143 | + |
48 | 144 | private static class WrappedCallbackCache<T> // per-T memoized helper that allows GetOrCreateAsync<T> and GetOrCreateAsync<TState, T> to share an implementation |
49 | 145 | { |
50 | 146 | // 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 |
|
0 commit comments