Skip to content

Commit ae24a38

Browse files
authored
Add IAsyncCacheExt for .NET Standard (#572)
1 parent f196e79 commit ae24a38

File tree

6 files changed

+55
-4
lines changed

6 files changed

+55
-4
lines changed

BitFaster.Caching/IAsyncCacheExt.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
}

BitFaster.Caching/ICacheExt.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ namespace BitFaster.Caching
99
/// </summary>
1010
/// <typeparam name="K">The type of keys in the cache.</typeparam>
1111
/// <typeparam name="V">The type of values in the cache.</typeparam>
12+
/// <remarks>This interface enables .NET Standard to use cache methods added to ICache since v2.0. It will be removed in the next major version.</remarks>
1213
public interface ICacheExt<K, V> : ICache<K, V>
1314
{
1415
// Following methods were also defined in ICache with default interface implementation which only works for
1516
// certain build targets, for other build targets we will define them within this new interface to avoid breaking
1617
// existing clients.
18+
// backcompat: remove conditional compile
1719
#if !NETCOREAPP3_0_OR_GREATER
1820
/// <summary>
1921
/// Adds a key/value pair to the cache if the key does not already exist. Returns the new value, or the

BitFaster.Caching/Lfu/ConcurrentLfu.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace BitFaster.Caching.Lfu
3232
/// https://github.com/ben-manes/caffeine
3333
[DebuggerTypeProxy(typeof(ConcurrentLfu<,>.LfuDebugView<>))]
3434
[DebuggerDisplay("Count = {Count}/{Capacity}")]
35-
public sealed class ConcurrentLfu<K, V> : ICacheExt<K, V>, IAsyncCache<K, V>, IBoundedPolicy
35+
public sealed class ConcurrentLfu<K, V> : ICacheExt<K, V>, IAsyncCacheExt<K, V>, IBoundedPolicy
3636
where K : notnull
3737
{
3838
// Note: for performance reasons this is a mutable struct, it cannot be readonly.

BitFaster.Caching/Lfu/ConcurrentTLfu.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace BitFaster.Caching.Lfu
1010
{
1111
// LFU with time-based expiry policy. Provided as a wrapper around ConcurrentLfuCore to hide generic item and policy.
12-
internal sealed class ConcurrentTLfu<K, V> : ICacheExt<K, V>, IAsyncCache<K, V>, IBoundedPolicy, ITimePolicy, IDiscreteTimePolicy
12+
internal sealed class ConcurrentTLfu<K, V> : ICacheExt<K, V>, IAsyncCacheExt<K, V>, IBoundedPolicy, ITimePolicy, IDiscreteTimePolicy
1313
where K : notnull
1414
{
1515
// Note: for performance reasons this is a mutable struct, it cannot be readonly.

BitFaster.Caching/Lru/ClassicLru.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace BitFaster.Caching.Lru
1818
/// </remarks>
1919
/// <typeparam name="K">The type of the key</typeparam>
2020
/// <typeparam name="V">The type of the value</typeparam>
21-
public sealed class ClassicLru<K, V> : ICacheExt<K, V>, IAsyncCache<K, V>, IBoundedPolicy, IEnumerable<KeyValuePair<K, V>>
21+
public sealed class ClassicLru<K, V> : ICacheExt<K, V>, IAsyncCacheExt<K, V>, IBoundedPolicy, IEnumerable<KeyValuePair<K, V>>
2222
where K : notnull
2323
{
2424
private readonly int capacity;

BitFaster.Caching/Lru/ConcurrentLruCore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace BitFaster.Caching.Lru
3232
/// <item><description>When cold is full, cold tail is moved to warm head or removed from dictionary on depending on WasAccessed.</description></item>
3333
///</list>
3434
/// </remarks>
35-
public class ConcurrentLruCore<K, V, I, P, T> : ICacheExt<K, V>, IAsyncCache<K, V>, IEnumerable<KeyValuePair<K, V>>
35+
public class ConcurrentLruCore<K, V, I, P, T> : ICacheExt<K, V>, IAsyncCacheExt<K, V>, IEnumerable<KeyValuePair<K, V>>
3636
where K : notnull
3737
where I : LruItem<K, V>
3838
where P : struct, IItemPolicy<K, V, I>

0 commit comments

Comments
 (0)