Skip to content

Commit ea94125

Browse files
author
Nate McMaster
committed
Reorganize source code in preparation to move into aspnet/Extensions
Prior to reorganization, this source code was found in https://github.com/aspnet/Caching/tree/dotnet/extensions@ced279b071920f055a309cd81acc333780ef2bf4 \n\nCommit migrated from dotnet/extensions@bacd21a
0 parents  commit ea94125

File tree

98 files changed

+12275
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+12275
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<Project>
2+
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory)..\, Directory.Build.props))\Directory.Build.props" />
3+
4+
<PropertyGroup>
5+
<IsProductComponent>true</IsProductComponent>
6+
</PropertyGroup>
7+
</Project>
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using Microsoft.Extensions.Primitives;
6+
7+
namespace Microsoft.Extensions.Caching.Memory
8+
{
9+
public static class CacheEntryExtensions
10+
{
11+
/// <summary>
12+
/// Sets the priority for keeping the cache entry in the cache during a memory pressure tokened cleanup.
13+
/// </summary>
14+
/// <param name="entry"></param>
15+
/// <param name="priority"></param>
16+
public static ICacheEntry SetPriority(
17+
this ICacheEntry entry,
18+
CacheItemPriority priority)
19+
{
20+
entry.Priority = priority;
21+
return entry;
22+
}
23+
24+
/// <summary>
25+
/// Expire the cache entry if the given <see cref="IChangeToken"/> expires.
26+
/// </summary>
27+
/// <param name="entry">The <see cref="ICacheEntry"/>.</param>
28+
/// <param name="expirationToken">The <see cref="IChangeToken"/> that causes the cache entry to expire.</param>
29+
public static ICacheEntry AddExpirationToken(
30+
this ICacheEntry entry,
31+
IChangeToken expirationToken)
32+
{
33+
if (expirationToken == null)
34+
{
35+
throw new ArgumentNullException(nameof(expirationToken));
36+
}
37+
38+
entry.ExpirationTokens.Add(expirationToken);
39+
return entry;
40+
}
41+
42+
/// <summary>
43+
/// Sets an absolute expiration time, relative to now.
44+
/// </summary>
45+
/// <param name="entry"></param>
46+
/// <param name="relative"></param>
47+
public static ICacheEntry SetAbsoluteExpiration(
48+
this ICacheEntry entry,
49+
TimeSpan relative)
50+
{
51+
entry.AbsoluteExpirationRelativeToNow = relative;
52+
return entry;
53+
}
54+
55+
/// <summary>
56+
/// Sets an absolute expiration date for the cache entry.
57+
/// </summary>
58+
/// <param name="entry"></param>
59+
/// <param name="absolute"></param>
60+
public static ICacheEntry SetAbsoluteExpiration(
61+
this ICacheEntry entry,
62+
DateTimeOffset absolute)
63+
{
64+
entry.AbsoluteExpiration = absolute;
65+
return entry;
66+
}
67+
68+
/// <summary>
69+
/// Sets how long the cache entry can be inactive (e.g. not accessed) before it will be removed.
70+
/// This will not extend the entry lifetime beyond the absolute expiration (if set).
71+
/// </summary>
72+
/// <param name="entry"></param>
73+
/// <param name="offset"></param>
74+
public static ICacheEntry SetSlidingExpiration(
75+
this ICacheEntry entry,
76+
TimeSpan offset)
77+
{
78+
entry.SlidingExpiration = offset;
79+
return entry;
80+
}
81+
82+
/// <summary>
83+
/// The given callback will be fired after the cache entry is evicted from the cache.
84+
/// </summary>
85+
/// <param name="entry"></param>
86+
/// <param name="callback"></param>
87+
public static ICacheEntry RegisterPostEvictionCallback(
88+
this ICacheEntry entry,
89+
PostEvictionDelegate callback)
90+
{
91+
if (callback == null)
92+
{
93+
throw new ArgumentNullException(nameof(callback));
94+
}
95+
96+
return entry.RegisterPostEvictionCallback(callback, state: null);
97+
}
98+
99+
/// <summary>
100+
/// The given callback will be fired after the cache entry is evicted from the cache.
101+
/// </summary>
102+
/// <param name="entry"></param>
103+
/// <param name="callback"></param>
104+
/// <param name="state"></param>
105+
public static ICacheEntry RegisterPostEvictionCallback(
106+
this ICacheEntry entry,
107+
PostEvictionDelegate callback,
108+
object state)
109+
{
110+
if (callback == null)
111+
{
112+
throw new ArgumentNullException(nameof(callback));
113+
}
114+
115+
entry.PostEvictionCallbacks.Add(new PostEvictionCallbackRegistration()
116+
{
117+
EvictionCallback = callback,
118+
State = state
119+
});
120+
return entry;
121+
}
122+
123+
/// <summary>
124+
/// Sets the value of the cache entry.
125+
/// </summary>
126+
/// <param name="entry"></param>
127+
/// <param name="value"></param>
128+
public static ICacheEntry SetValue(
129+
this ICacheEntry entry,
130+
object value)
131+
{
132+
entry.Value = value;
133+
return entry;
134+
}
135+
136+
/// <summary>
137+
/// Sets the size of the cache entry value.
138+
/// </summary>
139+
/// <param name="entry"></param>
140+
/// <param name="size"></param>
141+
public static ICacheEntry SetSize(
142+
this ICacheEntry entry,
143+
long size)
144+
{
145+
if (size < 0)
146+
{
147+
throw new ArgumentOutOfRangeException(nameof(size), size, $"{nameof(size)} must be non-negative.");
148+
}
149+
150+
entry.Size = size;
151+
return entry;
152+
}
153+
154+
/// <summary>
155+
/// Applies the values of an existing <see cref="MemoryCacheEntryOptions"/> to the entry.
156+
/// </summary>
157+
/// <param name="entry"></param>
158+
/// <param name="options"></param>
159+
public static ICacheEntry SetOptions(this ICacheEntry entry, MemoryCacheEntryOptions options)
160+
{
161+
if (options == null)
162+
{
163+
throw new ArgumentNullException(nameof(options));
164+
}
165+
166+
entry.AbsoluteExpiration = options.AbsoluteExpiration;
167+
entry.AbsoluteExpirationRelativeToNow = options.AbsoluteExpirationRelativeToNow;
168+
entry.SlidingExpiration = options.SlidingExpiration;
169+
entry.Priority = options.Priority;
170+
entry.Size = options.Size;
171+
172+
foreach (var expirationToken in options.ExpirationTokens)
173+
{
174+
entry.AddExpirationToken(expirationToken);
175+
}
176+
177+
foreach (var postEvictionCallback in options.PostEvictionCallbacks)
178+
{
179+
entry.RegisterPostEvictionCallback(postEvictionCallback.EvictionCallback, postEvictionCallback.State);
180+
}
181+
182+
return entry;
183+
}
184+
}
185+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
namespace Microsoft.Extensions.Caching.Memory
5+
{
6+
// TODO: Granularity?
7+
/// <summary>
8+
/// Specifies how items are prioritized for preservation during a memory pressure triggered cleanup.
9+
/// </summary>
10+
public enum CacheItemPriority
11+
{
12+
Low,
13+
Normal,
14+
High,
15+
NeverRemove,
16+
}
17+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
6+
namespace Microsoft.Extensions.Caching.Distributed
7+
{
8+
public static class DistributedCacheEntryExtensions
9+
{
10+
/// <summary>
11+
/// Sets an absolute expiration time, relative to now.
12+
/// </summary>
13+
/// <param name="options"></param>
14+
/// <param name="relative"></param>
15+
public static DistributedCacheEntryOptions SetAbsoluteExpiration(
16+
this DistributedCacheEntryOptions options,
17+
TimeSpan relative)
18+
{
19+
options.AbsoluteExpirationRelativeToNow = relative;
20+
return options;
21+
}
22+
23+
/// <summary>
24+
/// Sets an absolute expiration date for the cache entry.
25+
/// </summary>
26+
/// <param name="options"></param>
27+
/// <param name="absolute"></param>
28+
public static DistributedCacheEntryOptions SetAbsoluteExpiration(
29+
this DistributedCacheEntryOptions options,
30+
DateTimeOffset absolute)
31+
{
32+
options.AbsoluteExpiration = absolute;
33+
return options;
34+
}
35+
36+
/// <summary>
37+
/// Sets how long the cache entry can be inactive (e.g. not accessed) before it will be removed.
38+
/// This will not extend the entry lifetime beyond the absolute expiration (if set).
39+
/// </summary>
40+
/// <param name="options"></param>
41+
/// <param name="offset"></param>
42+
public static DistributedCacheEntryOptions SetSlidingExpiration(
43+
this DistributedCacheEntryOptions options,
44+
TimeSpan offset)
45+
{
46+
options.SlidingExpiration = offset;
47+
return options;
48+
}
49+
}
50+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
6+
namespace Microsoft.Extensions.Caching.Distributed
7+
{
8+
public class DistributedCacheEntryOptions
9+
{
10+
private DateTimeOffset? _absoluteExpiration;
11+
private TimeSpan? _absoluteExpirationRelativeToNow;
12+
private TimeSpan? _slidingExpiration;
13+
14+
/// <summary>
15+
/// Gets or sets an absolute expiration date for the cache entry.
16+
/// </summary>
17+
public DateTimeOffset? AbsoluteExpiration
18+
{
19+
get
20+
{
21+
return _absoluteExpiration;
22+
}
23+
set
24+
{
25+
_absoluteExpiration = value;
26+
}
27+
}
28+
29+
/// <summary>
30+
/// Gets or sets an absolute expiration time, relative to now.
31+
/// </summary>
32+
public TimeSpan? AbsoluteExpirationRelativeToNow
33+
{
34+
get
35+
{
36+
return _absoluteExpirationRelativeToNow;
37+
}
38+
set
39+
{
40+
if (value <= TimeSpan.Zero)
41+
{
42+
throw new ArgumentOutOfRangeException(
43+
nameof(AbsoluteExpirationRelativeToNow),
44+
value,
45+
"The relative expiration value must be positive.");
46+
}
47+
48+
_absoluteExpirationRelativeToNow = value;
49+
}
50+
}
51+
52+
/// <summary>
53+
/// Gets or sets how long a cache entry can be inactive (e.g. not accessed) before it will be removed.
54+
/// This will not extend the entry lifetime beyond the absolute expiration (if set).
55+
/// </summary>
56+
public TimeSpan? SlidingExpiration
57+
{
58+
get
59+
{
60+
return _slidingExpiration;
61+
}
62+
set
63+
{
64+
if (value <= TimeSpan.Zero)
65+
{
66+
throw new ArgumentOutOfRangeException(
67+
nameof(SlidingExpiration),
68+
value,
69+
"The sliding expiration value must be positive.");
70+
}
71+
_slidingExpiration = value;
72+
}
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)