Skip to content

Commit b88b7e3

Browse files
authored
Merge pull request #5 from Jakab-Laszlo/feature/memory-cache
Memory cache extensions +semver:minor
2 parents 280eba0 + 4654645 commit b88b7e3

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using Microsoft.Extensions.Caching.Memory;
2+
3+
namespace AutSoft.Common.Caching;
4+
5+
/// <summary>
6+
/// Extension functions for <see cref="IMemoryCache"/>
7+
/// </summary>
8+
public static class MemoryCacheExtensions
9+
{
10+
/// <summary>
11+
/// Get or create a cached element with error handling
12+
/// </summary>
13+
/// <typeparam name="TItem">The type of the cached element</typeparam>
14+
/// <param name="cache">An <see cref="IMemoryCache"/> object</param>
15+
/// <param name="key">The key of the cached element</param>
16+
/// <param name="factory">The factory method, which can create the element if it isn't cached</param>
17+
/// <param name="options">Options of the caching</param>
18+
/// <returns>The finded or created element</returns>
19+
public static async Task<TItem> GetOrCreateWithErrorHandlingAsync<TItem>(
20+
this IMemoryCache cache,
21+
string key,
22+
Func<ICacheEntry, Task<TItem>> factory,
23+
MemoryCacheEntryOptions? options = null)
24+
{
25+
try
26+
{
27+
return await cache.GetOrCreateAsync(key, entry =>
28+
{
29+
entry.SetOptions(options);
30+
return factory(entry);
31+
});
32+
}
33+
catch
34+
{
35+
cache.Remove(key);
36+
throw;
37+
}
38+
}
39+
40+
/// <summary>
41+
/// Create or override an element in the cache
42+
/// </summary>
43+
/// <typeparam name="TItem">The type of the cached element</typeparam>
44+
/// <param name="cache">An <see cref="IMemoryCache"/> object</param>
45+
/// <param name="key">The key of the cached element</param>
46+
/// <param name="value">The cached value</param>
47+
/// <param name="size">The size of the cached value</param>
48+
/// <param name="options">Options of the caching</param>
49+
public static void SetWithErrorHandling<TItem>(
50+
this IMemoryCache cache,
51+
string key,
52+
TItem value,
53+
int size,
54+
MemoryCacheEntryOptions? options = null)
55+
{
56+
try
57+
{
58+
using var entry = cache.CreateEntry(key);
59+
entry.SetOptions(options);
60+
entry.Size = size;
61+
entry.Value = value;
62+
}
63+
catch
64+
{
65+
cache.Remove(key);
66+
throw;
67+
}
68+
}
69+
70+
private static void SetOptions(this ICacheEntry entry, MemoryCacheEntryOptions? options = null)
71+
{
72+
if (options == null)
73+
{
74+
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(1);
75+
entry.SlidingExpiration = TimeSpan.FromMinutes(10);
76+
entry.Priority = CacheItemPriority.Normal;
77+
}
78+
else
79+
{
80+
entry.AbsoluteExpirationRelativeToNow = options.AbsoluteExpirationRelativeToNow;
81+
entry.SlidingExpiration = options.SlidingExpiration;
82+
entry.Priority = options.Priority;
83+
}
84+
}
85+
86+
/// <summary>
87+
/// Invalidate a cached element's value
88+
/// </summary>
89+
/// <param name="cache">An <see cref="IMemoryCache"/> object</param>
90+
/// <param name="key">The key of the cached element</param>
91+
public static void Invalidate(this IMemoryCache cache, string key)
92+
=> cache.Remove(key);
93+
}

0 commit comments

Comments
 (0)