Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Commit d8257be

Browse files
committed
Replacing IExpirationTrigger with IChangeToken
1 parent d16a7c1 commit d8257be

File tree

18 files changed

+220
-270
lines changed

18 files changed

+220
-270
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ nuget.exe
2525
*.*sdf
2626
*.ipch
2727
project.lock.json
28+
.vs

samples/MemoryCacheSample/Program.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Threading;
66
using Microsoft.Framework.Caching;
77
using Microsoft.Framework.Caching.Memory;
8+
using Microsoft.Framework.Primitives;
89

910
namespace MemoryCacheSample
1011
{
@@ -77,26 +78,26 @@ public void Main()
7778
});
7879
result = cache.Set(key, new object(), options);
7980

80-
// Remove on trigger
81+
// Remove on token expiration
8182
var cts = new CancellationTokenSource();
8283
options = new MemoryCacheEntryOptions()
83-
.AddExpirationTrigger(new CancellationTokenTrigger(cts.Token))
84+
.AddExpirationToken(new CancellationChangeToken(cts.Token))
8485
.RegisterPostEvictionCallback(
8586
(echoKey, value, reason, substate) =>
8687
{
8788
Console.WriteLine(echoKey + ": '" + value + "' was evicted due to " + reason);
8889
});
8990
result = cache.Set(key, new object(), options);
9091

91-
// Fire the trigger to see the registered callback being invoked
92+
// Fire the token to see the registered callback being invoked
9293
cts.Cancel();
9394

9495
// Expire an entry if the dependent entry expires
9596
using (var link = cache.CreateLinkingScope())
9697
{
9798
cts = new CancellationTokenSource();
9899
cache.Set("key1", "value1", new MemoryCacheEntryOptions()
99-
.AddExpirationTrigger(new CancellationTokenTrigger(cts.Token)));
100+
.AddExpirationToken(new CancellationChangeToken(cts.Token)));
100101

101102
// expire this entry if the entry with key "key1" expires.
102103
cache.Set("key2", "value2", new MemoryCacheEntryOptions()
@@ -108,7 +109,7 @@ public void Main()
108109
}));
109110
}
110111

111-
// Fire the trigger to see the registered callback being invoked
112+
// Fire the token to see the registered callback being invoked
112113
cts.Cancel();
113114
}
114115
}

src/Microsoft.Framework.Caching.Abstractions/CancellationTokenTrigger.cs

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/Microsoft.Framework.Caching.Abstractions/EvictionReason.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public enum EvictionReason
2525
/// <summary>
2626
/// Event
2727
/// </summary>
28-
Triggered,
28+
TokenExpired,
2929

3030
/// <summary>
3131
/// GC, overflow

src/Microsoft.Framework.Caching.Abstractions/IEntryLink.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using Microsoft.Framework.Primitives;
67

78
namespace Microsoft.Framework.Caching.Memory
89
{
910
/// <summary>
10-
/// Used to flow expiration information from one entry to another. Triggers and minimum absolute expiration will
11-
/// be copied from the dependent entry to the parent entry. The parent entry will not expire if the
11+
/// Used to flow expiration information from one entry to another. <see cref="IChangeToken"/> instances and minimum absolute
12+
/// expiration will be copied from the dependent entry to the parent entry. The parent entry will not expire if the
1213
/// dependent entry is removed manually, removed due to memory pressure, or expires due to sliding expiration.
1314
/// </summary>
1415
public interface IEntryLink : IDisposable
@@ -19,15 +20,15 @@ public interface IEntryLink : IDisposable
1920
DateTimeOffset? AbsoluteExpiration { get; }
2021

2122
/// <summary>
22-
/// Gets all the triggers from the dependent entries.
23+
/// Gets all the <see cref="IChangeToken"/> instances from the dependent entries.
2324
/// </summary>
24-
IEnumerable<IExpirationTrigger> Triggers { get; }
25+
IEnumerable<IChangeToken> ExpirationTokens { get; }
2526

2627
/// <summary>
27-
/// Adds triggers from a dependent entries.
28+
/// Adds <see cref="IChangeToken"/> instances from a dependent entries.
2829
/// </summary>
29-
/// <param name="triggers"></param>
30-
void AddExpirationTriggers(IList<IExpirationTrigger> triggers);
30+
/// <param name="expirationTokens"><see cref="IChangeToken"/> instances from dependent entries.</param>
31+
void AddExpirationTokens(IList<IChangeToken> expirationTokens);
3132

3233
/// <summary>
3334
/// Sets the absolute expiration for from a dependent entry. The minimum value across all dependent entries

src/Microsoft.Framework.Caching.Abstractions/IExpirationTrigger.cs

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/Microsoft.Framework.Caching.Abstractions/MemoryCacheEntryExtensions.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using Microsoft.Framework.Primitives;
56

67
namespace Microsoft.Framework.Caching.Memory
78
{
89
public static class MemoryCacheEntryExtensions
910
{
1011
/// <summary>
11-
/// Sets the priority for keeping the cache entry in the cache during a memory pressure triggered cleanup.
12+
/// Sets the priority for keeping the cache entry in the cache during a memory pressure tokened cleanup.
1213
/// </summary>
1314
/// <param name="options"></param>
1415
/// <param name="priority"></param>
@@ -21,20 +22,20 @@ public static MemoryCacheEntryOptions SetPriority(
2122
}
2223

2324
/// <summary>
24-
/// Expire the cache entry if the given event occurs.
25+
/// Expire the cache entry if the given <see cref="IChangeToken"/> expires.
2526
/// </summary>
26-
/// <param name="options"></param>
27-
/// <param name="trigger"></param>
28-
public static MemoryCacheEntryOptions AddExpirationTrigger(
27+
/// <param name="options">The <see cref="MemoryCacheEntryOptions"/>.</param>
28+
/// <param name="expirationToken">The <see cref="IChangeToken"/> that causes the cache entry to expire.</param>
29+
public static MemoryCacheEntryOptions AddExpirationToken(
2930
this MemoryCacheEntryOptions options,
30-
IExpirationTrigger trigger)
31+
IChangeToken expirationToken)
3132
{
32-
if (trigger == null)
33+
if (expirationToken == null)
3334
{
34-
throw new ArgumentNullException(nameof(trigger));
35+
throw new ArgumentNullException(nameof(expirationToken));
3536
}
3637

37-
options.Triggers.Add(trigger);
38+
options.ExpirationTokens.Add(expirationToken);
3839
return options;
3940
}
4041

@@ -121,14 +122,14 @@ public static MemoryCacheEntryOptions RegisterPostEvictionCallback(
121122
}
122123

123124
/// <summary>
124-
/// Adds inherited trigger and absolute expiration information.
125+
/// Adds inherited token and absolute expiration information.
125126
/// </summary>
126127
/// <param name="link"></param>
127128
public static MemoryCacheEntryOptions AddEntryLink(this MemoryCacheEntryOptions options, IEntryLink link)
128129
{
129-
foreach (var trigger in link.Triggers)
130+
foreach (var expirationToken in link.ExpirationTokens)
130131
{
131-
options.AddExpirationTrigger(trigger);
132+
options.AddExpirationToken(expirationToken);
132133
}
133134

134135
if (link.AbsoluteExpiration.HasValue)

src/Microsoft.Framework.Caching.Abstractions/MemoryCacheEntryOptions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using Microsoft.Framework.Primitives;
67

78
namespace Microsoft.Framework.Caching.Memory
89
{
@@ -74,9 +75,9 @@ public TimeSpan? SlidingExpiration
7475
}
7576

7677
/// <summary>
77-
/// Gets or sets the events which are fired when the cache entry expires.
78+
/// Gets the <see cref="IChangeToken"/> instances which cause the cache entry to expire.
7879
/// </summary>
79-
public IList<IExpirationTrigger> Triggers { get; } = new List<IExpirationTrigger>();
80+
public IList<IChangeToken> ExpirationTokens { get; } = new List<IChangeToken>();
8081

8182
/// <summary>
8283
/// Gets or sets the callbacks will be fired after the cache entry is evicted from the cache.

src/Microsoft.Framework.Caching.Abstractions/project.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
"warningsAsErrors": true
44
},
55
"description": "ASP.NET 5 caching abstractions.",
6+
"dependencies": {
7+
"Microsoft.Framework.Primitives": "1.0.0-*"
8+
},
69
"frameworks": {
710
"net45": { },
811
"dnx451": { },
912
"dotnet": {
1013
"dependencies": {
1114
"System.Collections": "4.0.11-beta-*",
12-
"System.Resources.ResourceManager": "4.0.1-beta-*",
1315
"System.Threading": "4.0.11-beta-*",
1416
"System.Threading.Tasks": "4.0.11-beta-*"
1517
}

src/Microsoft.Framework.Caching.Memory/CacheEntry.cs

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Microsoft.Framework.Caching.Memory
1010
{
1111
internal class CacheEntry
1212
{
13-
private static readonly Action<object> ExpirationCallback = TriggerExpired;
13+
private static readonly Action<object> ExpirationCallback = ExpirationTokensExpired;
1414

1515
private readonly Action<CacheEntry> _notifyCacheOfExpiration;
1616

@@ -43,15 +43,15 @@ internal CacheEntry(
4343

4444
internal EvictionReason EvictionReason { get; private set; }
4545

46-
internal IList<IDisposable> TriggerRegistrations { get; set; }
46+
internal IList<IDisposable> ExpirationTokenRegistrations { get; set; }
4747

4848
internal IList<PostEvictionCallbackRegistration> PostEvictionCallbacks { get; set; }
4949

5050
internal DateTimeOffset LastAccessed { get; set; }
5151

5252
internal bool CheckExpired(DateTimeOffset now)
5353
{
54-
return IsExpired || CheckForExpiredTime(now) || CheckForExpiredTriggers();
54+
return IsExpired || CheckForExpiredTime(now) || CheckForExpiredTokens();
5555
}
5656

5757
internal void SetExpired(EvictionReason reason)
@@ -61,7 +61,7 @@ internal void SetExpired(EvictionReason reason)
6161
{
6262
EvictionReason = reason;
6363
}
64-
DetachTriggers();
64+
DetachTokens();
6565
}
6666

6767
private bool CheckForExpiredTime(DateTimeOffset now)
@@ -82,61 +82,61 @@ private bool CheckForExpiredTime(DateTimeOffset now)
8282
return false;
8383
}
8484

85-
internal bool CheckForExpiredTriggers()
85+
internal bool CheckForExpiredTokens()
8686
{
87-
var triggers = Options.Triggers;
88-
if (triggers != null)
87+
var expiredTokens = Options.ExpirationTokens;
88+
if (expiredTokens != null)
8989
{
90-
for (int i = 0; i < triggers.Count; i++)
90+
for (int i = 0; i < expiredTokens.Count; i++)
9191
{
92-
var trigger = triggers[i];
93-
if (trigger.IsExpired)
92+
var expiredToken = expiredTokens[i];
93+
if (expiredToken.HasChanged)
9494
{
95-
SetExpired(EvictionReason.Triggered);
95+
SetExpired(EvictionReason.TokenExpired);
9696
return true;
9797
}
9898
}
9999
}
100100
return false;
101101
}
102102

103-
// TODO: There's a possible race between AttachTriggers and DetachTriggers if a trigger fires almost immediately.
103+
// TODO: There's a possible race between AttachTokens and DetachTokens if a token fires almost immediately.
104104
// This may result in some registrations not getting disposed.
105-
internal void AttachTriggers()
105+
internal void AttachTokens()
106106
{
107-
var triggers = Options.Triggers;
108-
if (triggers != null)
107+
var expirationTokens = Options.ExpirationTokens;
108+
if (expirationTokens != null)
109109
{
110-
for (int i = 0; i < triggers.Count; i++)
110+
for (int i = 0; i < expirationTokens.Count; i++)
111111
{
112-
var trigger = triggers[i];
113-
if (trigger.ActiveExpirationCallbacks)
112+
var expirationToken = expirationTokens[i];
113+
if (expirationToken.ActiveChangeCallbacks)
114114
{
115-
if (TriggerRegistrations == null)
115+
if (ExpirationTokenRegistrations == null)
116116
{
117-
TriggerRegistrations = new List<IDisposable>(1);
117+
ExpirationTokenRegistrations = new List<IDisposable>(1);
118118
}
119-
var registration = trigger.RegisterExpirationCallback(ExpirationCallback, this);
120-
TriggerRegistrations.Add(registration);
119+
var registration = expirationToken.RegisterChangeCallback(ExpirationCallback, this);
120+
ExpirationTokenRegistrations.Add(registration);
121121
}
122122
}
123123
}
124124
}
125125

126-
private static void TriggerExpired(object obj)
126+
private static void ExpirationTokensExpired(object obj)
127127
{
128128
var entry = (CacheEntry)obj;
129-
entry.SetExpired(EvictionReason.Triggered);
129+
entry.SetExpired(EvictionReason.TokenExpired);
130130
entry._notifyCacheOfExpiration(entry);
131131
}
132132

133133
// TODO: Thread safety
134-
private void DetachTriggers()
134+
private void DetachTokens()
135135
{
136-
var registrations = TriggerRegistrations;
136+
var registrations = ExpirationTokenRegistrations;
137137
if (registrations != null)
138138
{
139-
TriggerRegistrations = null;
139+
ExpirationTokenRegistrations = null;
140140
for (int i = 0; i < registrations.Count; i++)
141141
{
142142
var registration = registrations[i];

0 commit comments

Comments
 (0)