| | | 1 | | using AmbientServices.Utilities; |
| | | 2 | | using System; |
| | | 3 | | using System.Threading; |
| | | 4 | | using System.Threading.Tasks; |
| | | 5 | | |
| | | 6 | | namespace AmbientServices; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// A class that provides caching using either a specified cache or the ambient shared cache (<see cref="IAmbientSharedC |
| | | 10 | | /// </summary> |
| | | 11 | | /// <remarks> |
| | | 12 | | /// <pitch>The front door library code uses for shared caching of serializable values: it namespaces every key with an o |
| | | 13 | | /// <pledge> |
| | | 14 | | /// Every operation delegates to the explicit cache supplied at construction or, when none was, to whatever <see cref="I |
| | | 15 | | /// When neither exists the call quietly succeeds without caching: retrieval reports not-found and stores are discarded. |
| | | 16 | | /// Items must be serializable and must not be disposable, exactly as <see cref="IAmbientSharedCache"/> requires and for |
| | | 17 | | /// All keys are prefixed with the owner type's name (or the supplied prefix) before reaching the underlying cache, so d |
| | | 18 | | /// Clearing clears the entire underlying cache, not merely this owner's entries. |
| | | 19 | | /// </pledge> |
| | | 20 | | /// <plan>A stateless pass-through: it holds only the optional explicit cache and the computed key prefix, resolves the |
| | | 21 | | /// </remarks> |
| | | 22 | | public class AmbientSharedCache |
| | | 23 | | { |
| | | 24 | | private static readonly AmbientService<IAmbientSharedCache> _Cache = Ambient.GetService<IAmbientSharedCache>(); |
| | | 25 | | |
| | | 26 | | private readonly Type _type; |
| | | 27 | | private readonly string _defaultCachePrefix; |
| | | 28 | | private readonly IAmbientSharedCache? _explicitCache; |
| | | 29 | | private readonly string _cacheKeyPrefix; |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Creates the AmbientSharedCache using the ambient cache service. |
| | | 33 | | /// </summary> |
| | | 34 | | /// <param name="ownerType">The <see cref="Type"/> for the owner.</param> |
| | | 35 | | /// <param name="cacheKeyPrefix">An optional cache key prefix for all items cached through this class. Uses the typ |
| | | 36 | | public AmbientSharedCache(Type ownerType, string? cacheKeyPrefix = null) |
| | | 37 | | : this(ownerType, null, cacheKeyPrefix) |
| | | 38 | | { |
| | | 39 | | } |
| | | 40 | | /// <summary> |
| | | 41 | | /// Creates the AmbientSharedCache using the specified cache service. |
| | | 42 | | /// </summary> |
| | | 43 | | /// <param name="ownerType">The <see cref="Type"/> for the owner.</param> |
| | | 44 | | /// <param name="cache">An explicit <see cref="IAmbientSharedCache"/> to use.</param> |
| | | 45 | | /// <param name="cacheKeyPrefix">An optional cache key prefix for all items cached through this class. Uses the typ |
| | | 46 | | public AmbientSharedCache(Type ownerType, IAmbientSharedCache? cache, string? cacheKeyPrefix = null) |
| | | 47 | | { |
| | | 48 | | _type = ownerType; |
| | | 49 | | _defaultCachePrefix = $"{_type.Name}-"; |
| | | 50 | | _explicitCache = cache; |
| | | 51 | | _cacheKeyPrefix = cacheKeyPrefix ?? _defaultCachePrefix; |
| | | 52 | | } |
| | | 53 | | /// <summary> |
| | | 54 | | /// Retrieves the item with the specified key from the cache (if possible). |
| | | 55 | | /// </summary> |
| | | 56 | | /// <typeparam name="T">The type of the cached object.</typeparam> |
| | | 57 | | /// <param name="itemKey">The unique key used when the object was cached.</param> |
| | | 58 | | /// <param name="refresh">An optional <see cref="TimeSpan"/> indicating the length of time to extend the lifespan of |
| | | 59 | | /// <param name="cancel">The optional <see cref="CancellationToken"/>.</param> |
| | | 60 | | /// <returns>The cached object, or null if it was not found in the cache.</returns> |
| | | 61 | | public ValueTask<T?> Retrieve<T>(string itemKey, TimeSpan? refresh = null, CancellationToken cancel = default) where |
| | | 62 | | { |
| | | 63 | | IAmbientSharedCache? cache = _explicitCache ?? _Cache.Local; |
| | | 64 | | if (cache == null) return TaskUtilities.ValueTaskFromResult<T?>(null); |
| | | 65 | | return cache.Retrieve<T>(_cacheKeyPrefix + itemKey, refresh, cancel); |
| | | 66 | | } |
| | | 67 | | /// <summary> |
| | | 68 | | /// Stores the specified item in the cache. |
| | | 69 | | /// </summary> |
| | | 70 | | /// <typeparam name="T">The type of the item to be cached.</typeparam> |
| | | 71 | | /// <param name="itemKey">A string that uniquely identifies the item being cached.</param> |
| | | 72 | | /// <param name="item">The item to be cached.</param> |
| | | 73 | | /// <param name="maxCacheDuration">An optional <see cref="TimeSpan"/> indicating the maximum amount of time to keep |
| | | 74 | | /// <param name="expiration">An optional <see cref="DateTime"/> indicating a fixed time for when the item should exp |
| | | 75 | | /// <param name="cancel">The optional <see cref="CancellationToken"/>.</param> |
| | | 76 | | /// <remarks> |
| | | 77 | | /// If both <paramref name="expiration"/> and <paramref name="maxCacheDuration"/> are set, the earlier expiration wi |
| | | 78 | | /// </remarks> |
| | | 79 | | public ValueTask Store<T>(string itemKey, T item, TimeSpan? maxCacheDuration = null, DateTime? expiration = null, Ca |
| | | 80 | | { |
| | | 81 | | IAmbientSharedCache? cache = _explicitCache ?? _Cache.Local; |
| | | 82 | | if (cache == null) return default; |
| | | 83 | | return cache.Store<T>(_cacheKeyPrefix + itemKey, item, maxCacheDuration, expiration, cancel); |
| | | 84 | | } |
| | | 85 | | /// <summary> |
| | | 86 | | /// Removes the specified item from the cache. |
| | | 87 | | /// </summary> |
| | | 88 | | /// <typeparam name="T">The type of the item to be cached.</typeparam> |
| | | 89 | | /// <param name="itemKey">A string that uniquely identifies the item being cached.</param> |
| | | 90 | | /// <param name="cancel">The optional <see cref="CancellationToken"/>.</param> |
| | | 91 | | public ValueTask Remove<T>(string itemKey, CancellationToken cancel = default) |
| | | 92 | | { |
| | | 93 | | IAmbientSharedCache? cache = _explicitCache ?? _Cache.Local; |
| | | 94 | | if (cache == null) return default; |
| | | 95 | | return cache.Remove<T>(_cacheKeyPrefix + itemKey, cancel); |
| | | 96 | | } |
| | | 97 | | /// <summary> |
| | | 98 | | /// Flushes everything from the cache. |
| | | 99 | | /// </summary> |
| | | 100 | | /// <param name="cancel">The optional <see cref="CancellationToken"/>.</param> |
| | | 101 | | public ValueTask Clear(CancellationToken cancel = default) |
| | | 102 | | { |
| | | 103 | | IAmbientSharedCache? cache = _explicitCache ?? _Cache.Local; |
| | | 104 | | if (cache == null) return default; |
| | | 105 | | return cache.Clear(cancel); |
| | | 106 | | } |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// A generic type-specific shared cache owner class. The name of the type is prepended to each cache key. |
| | | 111 | | /// </summary> |
| | | 112 | | /// <typeparam name="TOWNER">The type that owns the log messages.</typeparam> |
| | | 113 | | /// <remarks> |
| | | 114 | | /// <pitch>The usual way to declare a shared cache: the owner is a type parameter, so the key prefix is derived at compi |
| | | 115 | | /// <pledge><see cref="AmbientSharedCache"/></pledge> |
| | | 116 | | /// <plan>Passes <c>typeof(TOWNER)</c> to the base class; adds no behavior of its own.</plan> |
| | | 117 | | /// </remarks> |
| | | 118 | | public class AmbientSharedCache<TOWNER> : AmbientSharedCache |
| | | 119 | | { |
| | | 120 | | /// <summary> |
| | | 121 | | /// Creates the AmbientSharedCache using the ambient cache service. |
| | | 122 | | /// </summary> |
| | | 123 | | /// <param name="cacheKeyPrefix">An optional cache key prefix for all items cached through this class. Uses the typ |
| | | 124 | | public AmbientSharedCache(string? cacheKeyPrefix = null) |
| | | 125 | | : this(null, cacheKeyPrefix) |
| | | 126 | | { |
| | | 127 | | } |
| | | 128 | | /// <summary> |
| | | 129 | | /// Creates the AmbientSharedCache using the specified cache service. |
| | | 130 | | /// </summary> |
| | | 131 | | /// <param name="cache">An explicit <see cref="IAmbientSharedCache"/> to use.</param> |
| | | 132 | | /// <param name="cacheKeyPrefix">An optional cache key prefix for all items cached through this class. Uses the typ |
| | | 133 | | public AmbientSharedCache(IAmbientSharedCache? cache, string? cacheKeyPrefix = null) : base(typeof(TOWNER), cache, c |
| | | 134 | | { |
| | | 135 | | } |
| | | 136 | | } |
| | | 137 | | |
| | | 138 | | |
| | | 139 | | // Remove these obsolete classes in 2026 |
| | | 140 | | /// <summary> |
| | | 141 | | /// A class that provides caching using either a specified cache or the ambient shared cache (<see cref="IAmbientSharedC |
| | | 142 | | /// </summary> |
| | | 143 | | [Obsolete("Rename all references to AmbientSharedCache before 2026")] |
| | | 144 | | public class AmbientCache : AmbientSharedCache |
| | | 145 | | { |
| | | 146 | | /// <summary> |
| | | 147 | | /// Creates the AmbientSharedCache using the ambient cache service. |
| | | 148 | | /// </summary> |
| | | 149 | | /// <param name="ownerType">The <see cref="Type"/> for the owner.</param> |
| | | 150 | | /// <param name="cacheKeyPrefix">An optional cache key prefix for all items cached through this class. Uses the typ |
| | | 151 | | public AmbientCache(Type ownerType, string? cacheKeyPrefix = null) |
| | 2 | 152 | | : this(ownerType, null, cacheKeyPrefix) |
| | | 153 | | { |
| | 2 | 154 | | } |
| | | 155 | | /// <summary> |
| | | 156 | | /// Creates the AmbientSharedCache using the specified cache service. |
| | | 157 | | /// </summary> |
| | | 158 | | /// <param name="ownerType">The <see cref="Type"/> for the owner.</param> |
| | | 159 | | /// <param name="cache">An explicit <see cref="IAmbientSharedCache"/> to use.</param> |
| | | 160 | | /// <param name="cacheKeyPrefix">An optional cache key prefix for all items cached through this class. Uses the typ |
| | | 161 | | public AmbientCache(Type ownerType, IAmbientSharedCache? cache, string? cacheKeyPrefix = null) |
| | 2 | 162 | | : base (ownerType, cache, cacheKeyPrefix) |
| | | 163 | | { |
| | 2 | 164 | | } |
| | | 165 | | } |
| | | 166 | | /// <summary> |
| | | 167 | | /// A generic type-specific shared cache owner . The name of the type is prepended to each cache key. |
| | | 168 | | /// </summary> |
| | | 169 | | /// <typeparam name="TOWNER">The type that owns the log messages.</typeparam> |
| | | 170 | | [Obsolete("Rename all references to AmbientSharedCache before 2026")] |
| | | 171 | | public class AmbientCache<TOWNER> : AmbientSharedCache |
| | | 172 | | { |
| | | 173 | | /// <summary> |
| | | 174 | | /// Creates the AmbientCache using the ambient cache service. |
| | | 175 | | /// </summary> |
| | | 176 | | /// <param name="cacheKeyPrefix">An optional cache key prefix for all items cached through this class. Uses the typ |
| | | 177 | | public AmbientCache(string? cacheKeyPrefix = null) |
| | | 178 | | : this(null, cacheKeyPrefix) |
| | | 179 | | { |
| | | 180 | | } |
| | | 181 | | /// <summary> |
| | | 182 | | /// Creates the AmbientCache using the specified cache service. |
| | | 183 | | /// </summary> |
| | | 184 | | /// <param name="cache">An explicit <see cref="IAmbientSharedCache"/> to use.</param> |
| | | 185 | | /// <param name="cacheKeyPrefix">An optional cache key prefix for all items cached through this class. Uses the typ |
| | | 186 | | public AmbientCache(IAmbientSharedCache? cache, string? cacheKeyPrefix = null) : base(typeof(TOWNER), cache, cacheKe |
| | | 187 | | { |
| | | 188 | | } |
| | | 189 | | } |