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
+ }
0 commit comments