|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics.CodeAnalysis; |
| 4 | +using System.Threading.Tasks; |
| 5 | + |
| 6 | +namespace BitFaster.Caching |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Represents a generic cache of key/value pairs. |
| 10 | + /// </summary> |
| 11 | + /// <typeparam name="K">The type of keys in the cache.</typeparam> |
| 12 | + /// <typeparam name="V">The type of values in the cache.</typeparam> |
| 13 | + /// <remarks>This interface enables .NET Standard to use cache methods added to IAsyncCache since v2.0. It will be removed in the next major version.</remarks> |
| 14 | + public interface IAsyncCacheExt<K, V> : IAsyncCache<K, V> |
| 15 | + { |
| 16 | + // Following methods were also defined in ICache with default interface implementation which only works for |
| 17 | + // certain build targets, for other build targets we will define them within this new interface to avoid breaking |
| 18 | + // existing clients. |
| 19 | +// backcompat: remove conditional compile |
| 20 | +#if !NETCOREAPP3_0_OR_GREATER |
| 21 | + /// <summary> |
| 22 | + /// Adds a key/value pair to the cache if the key does not already exist. Returns the new value, or the |
| 23 | + /// existing value if the key already exists. |
| 24 | + /// </summary> |
| 25 | + /// <typeparam name="TArg">The type of an argument to pass into valueFactory.</typeparam> |
| 26 | + /// <param name="key">The key of the element to add.</param> |
| 27 | + /// <param name="valueFactory">The factory function used to asynchronously generate a value for the key.</param> |
| 28 | + /// <param name="factoryArgument">An argument value to pass into valueFactory.</param> |
| 29 | + /// <returns>A task that represents the asynchronous GetOrAdd operation.</returns> |
| 30 | + /// <remarks>The default implementation given here is the fallback that provides backwards compatibility for classes that implement ICache on prior versions</remarks> |
| 31 | + ValueTask<V> GetOrAddAsync<TArg>(K key, Func<K, TArg, Task<V>> valueFactory, TArg factoryArgument); |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Attempts to remove and return the value that has the specified key. |
| 35 | + /// </summary> |
| 36 | + /// <param name="key">The key of the element to remove.</param> |
| 37 | + /// <param name="value">When this method returns, contains the object removed, or the default value of the value type if key does not exist.</param> |
| 38 | + /// <returns>true if the object was removed successfully; otherwise, false.</returns> |
| 39 | + bool TryRemove(K key, [MaybeNullWhen(false)] out V value); |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Attempts to remove the specified key value pair. |
| 43 | + /// </summary> |
| 44 | + /// <param name="item">The item to remove.</param> |
| 45 | + /// <returns>true if the item was removed successfully; otherwise, false.</returns> |
| 46 | + bool TryRemove(KeyValuePair<K, V> item); |
| 47 | +#endif |
| 48 | + } |
| 49 | +} |
0 commit comments