/
MemoryCacheEntryExtensions.cs
143 lines (130 loc) · 6.34 KB
/
MemoryCacheEntryExtensions.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using Microsoft.Extensions.Primitives;
namespace Microsoft.Extensions.Caching.Memory
{
/// <summary>
/// Provide extensions methods for <see cref="MemoryCacheEntryOptions"/> operations.
/// </summary>
public static class MemoryCacheEntryExtensions
{
/// <summary>
/// Sets the priority for keeping the cache entry in the cache during a memory pressure tokened cleanup.
/// </summary>
/// <param name="options">The option on which to set the priority.</param>
/// <param name="priority">The <see cref="CacheItemPriority"/> to set on the option.</param>
/// <returns>The <see cref="MemoryCacheEntryOptions"/> so that additional calls can be chained.</returns>
public static MemoryCacheEntryOptions SetPriority(
this MemoryCacheEntryOptions options,
CacheItemPriority priority)
{
options.Priority = priority;
return options;
}
/// <summary>
/// Sets the size of the cache entry value.
/// </summary>
/// <param name="options">The options to set the entry size on.</param>
/// <param name="size">The size to set on the <see cref="MemoryCacheEntryOptions"/>.</param>
/// <returns>The <see cref="MemoryCacheEntryOptions"/> so that additional calls can be chained.</returns>
public static MemoryCacheEntryOptions SetSize(
this MemoryCacheEntryOptions options,
long size)
{
if (size < 0)
{
throw new ArgumentOutOfRangeException(nameof(size), size, $"{nameof(size)} must be non-negative.");
}
options.Size = size;
return options;
}
/// <summary>
/// Expire the cache entry if the given <see cref="IChangeToken"/> expires.
/// </summary>
/// <param name="options">The <see cref="MemoryCacheEntryOptions"/>.</param>
/// <param name="expirationToken">The <see cref="IChangeToken"/> that causes the cache entry to expire.</param>
/// <returns>The <see cref="MemoryCacheEntryOptions"/> so that additional calls can be chained.</returns>
public static MemoryCacheEntryOptions AddExpirationToken(
this MemoryCacheEntryOptions options,
IChangeToken expirationToken)
{
ThrowHelper.ThrowIfNull(expirationToken);
options.ExpirationTokens.Add(expirationToken);
return options;
}
/// <summary>
/// Sets an absolute expiration time, relative to now.
/// </summary>
/// <param name="options">The <see cref="MemoryCacheEntryOptions"/>.</param>
/// <param name="relative">The expiration time, relative to now.</param>
/// <returns>The <see cref="MemoryCacheEntryOptions"/> so that additional calls can be chained.</returns>
public static MemoryCacheEntryOptions SetAbsoluteExpiration(
this MemoryCacheEntryOptions options,
TimeSpan relative)
{
options.AbsoluteExpirationRelativeToNow = relative;
return options;
}
/// <summary>
/// Sets an absolute expiration date for the cache entry.
/// </summary>
/// <param name="options">The <see cref="MemoryCacheEntryOptions"/>.</param>
/// <param name="absolute">The expiration time, in absolute terms.</param>
/// <returns>The <see cref="MemoryCacheEntryOptions"/> so that additional calls can be chained.</returns>
public static MemoryCacheEntryOptions SetAbsoluteExpiration(
this MemoryCacheEntryOptions options,
DateTimeOffset absolute)
{
options.AbsoluteExpiration = absolute;
return options;
}
/// <summary>
/// Sets how long the cache entry can be inactive (e.g. not accessed) before it will be removed.
/// This will not extend the entry lifetime beyond the absolute expiration (if set).
/// </summary>
/// <param name="options">The <see cref="MemoryCacheEntryOptions"/>.</param>
/// <param name="offset">The sliding expiration time.</param>
/// <returns>The <see cref="MemoryCacheEntryOptions"/> so that additional calls can be chained.</returns>
public static MemoryCacheEntryOptions SetSlidingExpiration(
this MemoryCacheEntryOptions options,
TimeSpan offset)
{
options.SlidingExpiration = offset;
return options;
}
/// <summary>
/// The given callback will be fired after the cache entry is evicted from the cache.
/// </summary>
/// <param name="options">The <see cref="MemoryCacheEntryOptions"/>.</param>
/// <param name="callback">The callback to register for calling after an entry is evicted.</param>
/// <returns>The <see cref="MemoryCacheEntryOptions"/> so that additional calls can be chained.</returns>
public static MemoryCacheEntryOptions RegisterPostEvictionCallback(
this MemoryCacheEntryOptions options,
PostEvictionDelegate callback)
{
ThrowHelper.ThrowIfNull(callback);
return options.RegisterPostEvictionCallback(callback, state: null);
}
/// <summary>
/// The given callback will be fired after the cache entry is evicted from the cache.
/// </summary>
/// <param name="options">The <see cref="MemoryCacheEntryOptions"/>.</param>
/// <param name="callback">The callback to register for calling after an entry is evicted.</param>
/// <param name="state">The state to pass to the callback.</param>
/// <returns>The <see cref="MemoryCacheEntryOptions"/> so that additional calls can be chained.</returns>
public static MemoryCacheEntryOptions RegisterPostEvictionCallback(
this MemoryCacheEntryOptions options,
PostEvictionDelegate callback,
object? state)
{
ThrowHelper.ThrowIfNull(callback);
options.PostEvictionCallbacks.Add(new PostEvictionCallbackRegistration()
{
EvictionCallback = callback,
State = state
});
return options;
}
}
}