|
| 1 | +<Query Kind="Program"> |
| 2 | + <NuGetReference>Microsoft.Extensions.Caching.Abstractions</NuGetReference> |
| 3 | + <Namespace>Microsoft.Extensions.Caching.Distributed</Namespace> |
| 4 | + <Namespace>System.Threading.Tasks</Namespace> |
| 5 | +</Query> |
| 6 | + |
| 7 | +void Main() |
| 8 | +{ |
| 9 | + |
| 10 | +} |
| 11 | + |
| 12 | +public class KeyPrefixedCache : IDistributedCache |
| 13 | +{ |
| 14 | + IDistributedCache _cache; |
| 15 | + string _prefix; |
| 16 | + |
| 17 | + public KeyPrefixedCache(IDistributedCache cache, string prefix) |
| 18 | + { |
| 19 | + _cache = cache; |
| 20 | + _prefix = prefix; |
| 21 | + } |
| 22 | + |
| 23 | + private string PrefixKey(string key) => $"{_prefix}_{key}"; |
| 24 | + |
| 25 | + public byte[] Get(string key) => |
| 26 | + _cache.Get(PrefixKey(key)); |
| 27 | + |
| 28 | + public Task<byte[]> GetAsync(string key, CancellationToken token = default) => |
| 29 | + _cache.GetAsync(PrefixKey(key), token) |
| 30 | + |
| 31 | + public void Refresh(string key) => |
| 32 | + _cache.Refresh(PrefixKey(key)); |
| 33 | + |
| 34 | + public Task RefreshAsync(string key, CancellationToken token = default) => |
| 35 | + _cache.RefreshAsync(PrefixKey(key), token); |
| 36 | + |
| 37 | + public void Remove(string key) => |
| 38 | + _cache.Remove(PrefixKey(key)); |
| 39 | + |
| 40 | + public Task RemoveAsync(string key, CancellationToken token = default) => |
| 41 | + _cache.RemoveAsync(PrefixKey(key), token) |
| 42 | + |
| 43 | + public void Set(string key, byte[] value, DistributedCacheEntryOptions options) => |
| 44 | + _cache.Set(PrefixKey(key), value, options); |
| 45 | + |
| 46 | + public Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options, CancellationToken token = default) => |
| 47 | + _cache.SetAsync(PrefixKey(key), value, options, token); |
| 48 | +} |
0 commit comments