This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
/
MemoryCacheExtensions.cs
128 lines (107 loc) · 3.74 KB
/
MemoryCacheExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Primitives;
namespace Microsoft.Extensions.Caching.Memory
{
public static class CacheExtensions
{
public static object Get(this IMemoryCache cache, object key)
{
object value = null;
cache.TryGetValue(key, out value);
return value;
}
public static TItem Get<TItem>(this IMemoryCache cache, object key)
{
TItem value;
cache.TryGetValue<TItem>(key, out value);
return value;
}
public static bool TryGetValue<TItem>(this IMemoryCache cache, object key, out TItem value)
{
object result;
if (cache.TryGetValue(key, out result))
{
value = (TItem)result;
return true;
}
value = default(TItem);
return false;
}
public static TItem Set<TItem>(this IMemoryCache cache, object key, TItem value)
{
using (var entry = cache.CreateEntry(key))
{
entry.Value = value;
}
return value;
}
public static TItem Set<TItem>(this IMemoryCache cache, object key, TItem value, DateTimeOffset absoluteExpiration)
{
using (var entry = cache.CreateEntry(key))
{
entry.AbsoluteExpiration = absoluteExpiration;
entry.Value = value;
}
return value;
}
public static TItem Set<TItem>(this IMemoryCache cache, object key, TItem value, TimeSpan absoluteExpirationRelativeToNow)
{
using (var entry = cache.CreateEntry(key))
{
entry.AbsoluteExpirationRelativeToNow = absoluteExpirationRelativeToNow;
entry.Value = value;
}
return value;
}
public static TItem Set<TItem>(this IMemoryCache cache, object key, TItem value, IChangeToken expirationToken)
{
using (var entry = cache.CreateEntry(key))
{
entry.AddExpirationToken(expirationToken);
entry.Value = value;
}
return value;
}
public static TItem Set<TItem>(this IMemoryCache cache, object key, TItem value, MemoryCacheEntryOptions options)
{
using (var entry = cache.CreateEntry(key))
{
if (options != null)
{
entry.SetOptions(options);
}
entry.Value = value;
}
return value;
}
public static TItem GetOrCreate<TItem>(this IMemoryCache cache, object key, Func<ICacheEntry, TItem> factory)
{
object result;
if (!cache.TryGetValue(key, out result))
{
using (var entry = cache.CreateEntry(key))
{
result = factory(entry);
entry.SetValue(result);
}
}
return (TItem)result;
}
public static async Task<TItem> GetOrCreateAsync<TItem>(this IMemoryCache cache, object key, Func<ICacheEntry, Task<TItem>> factory)
{
object result;
if (!cache.TryGetValue(key, out result))
{
using (var entry = cache.CreateEntry(key))
{
result = await factory(entry);
entry.SetValue(result);
}
}
return (TItem)result;
}
}
}