Skip to content

Commit 036e9ce

Browse files
committed
Initial
1 parent f8314ef commit 036e9ce

File tree

8 files changed

+329
-9
lines changed

8 files changed

+329
-9
lines changed

GenericMemoryCache/CacheEntry.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Microsoft.Extensions.Caching.Memory;
4+
using Microsoft.Extensions.Primitives;
5+
6+
namespace GenericMemoryCache
7+
{
8+
public class CacheEntry<TKey, TValue> : ICacheEntry<TKey, TValue>
9+
{
10+
public ICacheEntry Entry { get; }
11+
internal CacheEntry(ICacheEntry entry)
12+
{
13+
this.Entry = entry;
14+
}
15+
16+
public TKey Key => (TKey)this.Entry.Key;
17+
public TValue Value => (TValue)this.Entry.Value;
18+
19+
public DateTimeOffset? AbsoluteExpiration
20+
{
21+
get => this.Entry.AbsoluteExpiration;
22+
set => this.Entry.AbsoluteExpiration = value;
23+
}
24+
25+
public TimeSpan? AbsoluteExpirationRelativeToNow
26+
{
27+
get => this.Entry.AbsoluteExpirationRelativeToNow;
28+
set => this.Entry.AbsoluteExpirationRelativeToNow = value;
29+
}
30+
31+
public TimeSpan? SlidingExpiration
32+
{
33+
get => this.Entry.SlidingExpiration;
34+
set => this.Entry.SlidingExpiration = value;
35+
}
36+
37+
public IList<IChangeToken> ExpirationTokens => this.Entry.ExpirationTokens;
38+
39+
public IList<PostEvictionCallbackRegistration> PostEvictionCallbacks
40+
=> this.Entry.PostEvictionCallbacks;
41+
42+
public CacheItemPriority Priority
43+
{
44+
get => this.Entry.Priority;
45+
set => this.Entry.Priority = value;
46+
}
47+
}
48+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
using System;
2+
using Microsoft.Extensions.Caching.Memory;
3+
using Microsoft.Extensions.Primitives;
4+
5+
namespace GenericMemoryCache
6+
{
7+
public static class CacheEntryExtensions
8+
{
9+
public static ICacheEntry<TKey, TValue> ToGeneric<TKey, TValue>(ICacheEntry entry)
10+
=> new CacheEntry<TKey, TValue>(entry);
11+
12+
13+
/// <summary>
14+
/// Sets the priority for keeping the cache entry in the cache during a memory pressure tokened cleanup.
15+
/// </summary>
16+
/// <param name="entry"></param>
17+
/// <param name="priority"></param>
18+
public static ICacheEntry<TKey, TValue> SetPriority<TKey, TValue>(
19+
this ICacheEntry<TKey, TValue> entry,
20+
CacheItemPriority priority)
21+
{
22+
entry.Priority = priority;
23+
return entry;
24+
}
25+
26+
/// <summary>
27+
/// Expire the cache entry if the given <see cref="IChangeToken"/> expires.
28+
/// </summary>
29+
/// <param name="entry">The <see cref="ICacheEntry<TKey, TValue>"/>.</param>
30+
/// <param name="expirationToken">The <see cref="IChangeToken"/> that causes the cache entry to expire.</param>
31+
public static ICacheEntry<TKey, TValue> AddExpirationToken<TKey, TValue>(
32+
this ICacheEntry<TKey, TValue> entry,
33+
IChangeToken expirationToken)
34+
{
35+
if (expirationToken == null)
36+
{
37+
throw new ArgumentNullException(nameof(expirationToken));
38+
}
39+
40+
entry.ExpirationTokens.Add(expirationToken);
41+
return entry;
42+
}
43+
44+
/// <summary>
45+
/// Sets an absolute expiration time, relative to now.
46+
/// </summary>
47+
/// <param name="entry"></param>
48+
/// <param name="relative"></param>
49+
public static ICacheEntry<TKey, TValue> SetAbsoluteExpiration<TKey, TValue>(
50+
this ICacheEntry<TKey, TValue> entry,
51+
TimeSpan relative)
52+
{
53+
entry.AbsoluteExpirationRelativeToNow = relative;
54+
return entry;
55+
}
56+
57+
/// <summary>
58+
/// Sets an absolute expiration date for the cache entry.
59+
/// </summary>
60+
/// <param name="entry"></param>
61+
/// <param name="absolute"></param>
62+
public static ICacheEntry<TKey, TValue> SetAbsoluteExpiration<TKey, TValue>(
63+
this ICacheEntry<TKey, TValue> entry,
64+
DateTimeOffset absolute)
65+
{
66+
entry.AbsoluteExpiration = absolute;
67+
return entry;
68+
}
69+
70+
/// <summary>
71+
/// Sets how long the cache entry can be inactive (e.g. not accessed) before it will be removed.
72+
/// This will not extend the entry lifetime beyond the absolute expiration (if set).
73+
/// </summary>
74+
/// <param name="entry"></param>
75+
/// <param name="offset"></param>
76+
public static ICacheEntry<TKey, TValue> SetSlidingExpiration<TKey, TValue>(
77+
this ICacheEntry<TKey, TValue> entry,
78+
TimeSpan offset)
79+
{
80+
entry.SlidingExpiration = offset;
81+
return entry;
82+
}
83+
84+
/// <summary>
85+
/// The given callback will be fired after the cache entry is evicted from the cache.
86+
/// </summary>
87+
/// <param name="entry"></param>
88+
/// <param name="callback"></param>
89+
public static ICacheEntry<TKey, TValue> RegisterPostEvictionCallback<TKey, TValue>(
90+
this ICacheEntry<TKey, TValue> entry,
91+
PostEvictionDelegate callback)
92+
{
93+
entry.Entry.RegisterPostEvictionCallback(callback);
94+
return entry;
95+
}
96+
97+
/// <summary>
98+
/// The given callback will be fired after the cache entry is evicted from the cache.
99+
/// </summary>
100+
/// <param name="entry"></param>
101+
/// <param name="callback"></param>
102+
/// <param name="state"></param>
103+
public static ICacheEntry<TKey, TValue> RegisterPostEvictionCallback<TKey, TValue>(
104+
this ICacheEntry<TKey, TValue> entry,
105+
PostEvictionDelegate callback,
106+
object state)
107+
{
108+
entry.Entry.RegisterPostEvictionCallback(callback, state);
109+
return entry;
110+
}
111+
112+
/// <summary>
113+
/// Sets the value of the cache entry.
114+
/// </summary>
115+
/// <param name="entry"></param>
116+
/// <param name="value"></param>
117+
public static ICacheEntry<TKey, TValue> SetValue<TKey, TValue>(
118+
this ICacheEntry<TKey, TValue> entry,
119+
TValue value)
120+
{
121+
entry.Entry.SetValue(value);
122+
return entry;
123+
}
124+
125+
/// <summary>
126+
/// Applies the values of an existing <see cref="MemoryCacheEntryOptions"/> to the entry.
127+
/// </summary>
128+
/// <param name="entry"></param>
129+
/// <param name="options"></param>
130+
public static ICacheEntry<TKey, TValue> SetOptions<TKey, TValue>(this ICacheEntry<TKey, TValue> entry, MemoryCacheEntryOptions options)
131+
{
132+
entry.Entry.SetOptions(options);
133+
return entry;
134+
}
135+
}
136+
}

GenericMemoryCache/Class1.cs

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>netstandard1.4</TargetFramework>
55
</PropertyGroup>
66

7+
<ItemGroup>
8+
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="1.1.2" />
9+
</ItemGroup>
10+
711
</Project>

GenericMemoryCache/ICacheEntry.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Microsoft.Extensions.Caching.Memory;
4+
using Microsoft.Extensions.Primitives;
5+
6+
namespace GenericMemoryCache
7+
{
8+
/// <summary>
9+
/// Generic CacheEntry
10+
/// </summary>
11+
/// <typeparam name="TKey">Type of key</typeparam>
12+
/// <typeparam name="TValue">Type of value</typeparam>
13+
public interface ICacheEntry<out TKey, out TValue>
14+
{
15+
ICacheEntry Entry { get; }
16+
17+
TKey Key { get; }
18+
TValue Value { get; }
19+
DateTimeOffset? AbsoluteExpiration { get; set; }
20+
TimeSpan? AbsoluteExpirationRelativeToNow { get; set; }
21+
TimeSpan? SlidingExpiration { get; set; }
22+
IList<IChangeToken> ExpirationTokens { get; }
23+
IList<PostEvictionCallbackRegistration> PostEvictionCallbacks { get; }
24+
CacheItemPriority Priority { get; set; }
25+
}
26+
}

GenericMemoryCache/IMemoryCache.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Microsoft.Extensions.Caching.Memory;
2+
3+
namespace GenericMemoryCache
4+
{
5+
/// <summary>
6+
/// Generic な MemoryCache です
7+
/// </summary>
8+
/// <typeparam name="TKey">Type of cache key</typeparam>
9+
/// <typeparam name="TValue">Type of value</typeparam>
10+
public interface IMemoryCache<TKey, TValue>
11+
{
12+
IMemoryCache Cache { get; }
13+
14+
CacheEntry<TKey, TValue> CreateEntry(TKey key);
15+
void Remove(TKey key);
16+
bool TryGetValue(TKey key, out TValue value);
17+
}
18+
}

GenericMemoryCache/MemoryCache.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using Microsoft.Extensions.Caching.Memory;
3+
4+
namespace GenericMemoryCache
5+
{
6+
public class MemoryCache<TKey, TValue> : IMemoryCache<TKey, TValue>, IDisposable
7+
{
8+
public IMemoryCache Cache { get; }
9+
10+
internal MemoryCache(IMemoryCache cache)
11+
{
12+
this.Cache = cache;
13+
}
14+
15+
//public int Count => Cache.Count;
16+
17+
//public void Compact(double percentage) => Cache.Compact(percentage);
18+
19+
public CacheEntry<TKey, TValue> CreateEntry(TKey key)
20+
=> new CacheEntry<TKey,TValue>(this.Cache.CreateEntry(key));
21+
22+
public void Dispose() => this.Cache.Dispose();
23+
24+
public void Remove(TKey key) => this.Cache.Remove(key);
25+
26+
public bool TryGetValue(TKey key, out TValue result)
27+
=> this.Cache.TryGetValue<TValue>(key, out result);
28+
}
29+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Microsoft.Extensions.Caching.Memory;
4+
using Microsoft.Extensions.Primitives;
5+
6+
namespace GenericMemoryCache
7+
{
8+
public static class MemoryCacheExtensions
9+
{
10+
public static MemoryCache<TKey, TValue> ToGeneric<TKey, TValue>(this IMemoryCache cache)
11+
=> new MemoryCache<TKey, TValue>(cache);
12+
13+
public static TValue Get<TKey, TValue>(this IMemoryCache<TKey, TValue> cache, TKey key)
14+
{
15+
cache.TryGetValue(key, out TValue value);
16+
return value;
17+
}
18+
19+
public static TValue Set<TKey, TValue>(this IMemoryCache<TKey, TValue> cache, TKey key, TValue value)
20+
{
21+
cache.Cache.Set(key, value);
22+
return value;
23+
}
24+
25+
public static TValue Set<TKey, TValue>(this IMemoryCache<TKey, TValue> cache, TKey key, TValue value, DateTimeOffset absoluteExpiration)
26+
{
27+
cache.Cache.Set(key, value, absoluteExpiration);
28+
return value;
29+
}
30+
31+
public static TValue Set<TKey, TValue>(this IMemoryCache<TKey, TValue> cache, TKey key, TValue value, TimeSpan absoluteExpirationRelativeToNow)
32+
{
33+
cache.Cache.Set(key, value, absoluteExpirationRelativeToNow);
34+
return value;
35+
}
36+
37+
public static TValue Set<TKey, TValue>(this IMemoryCache<TKey, TValue> cache, TKey key, TValue value, IChangeToken expirationToken)
38+
{
39+
cache.Cache.Set(key, value, expirationToken);
40+
return value;
41+
}
42+
43+
public static TValue Set<TKey, TValue>(this IMemoryCache<TKey, TValue> cache, TKey key, TValue value, MemoryCacheEntryOptions options)
44+
{
45+
cache.Cache.Set(key, value, options);
46+
return value;
47+
}
48+
49+
public static TValue GetOrCreate<TKey, TValue>(this IMemoryCache<TKey, TValue> cache, TKey key,
50+
Func<ICacheEntry<TKey, TValue>, TValue> factory)
51+
{
52+
TValue ObjectFactory(ICacheEntry entry)
53+
=> factory(new CacheEntry<TKey, TValue>(entry));
54+
55+
return cache.Cache.GetOrCreate<TValue>(key, ObjectFactory);
56+
}
57+
58+
public static async Task<TValue> GetOrCreateAsync<TKey, TValue>(this IMemoryCache<TKey, TValue> cache, TKey key,
59+
Func<ICacheEntry<TKey, TValue>, Task<TValue>> factory)
60+
{
61+
Task<TValue> ObjectFactory(ICacheEntry entry)
62+
=> factory(new CacheEntry<TKey, TValue>(entry));
63+
64+
return await cache.Cache.GetOrCreateAsync(key, ObjectFactory);
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)