-
Notifications
You must be signed in to change notification settings - Fork 217
Conversation
public interface IMemoryCacheEvictionStrategy | ||
{ | ||
// TODO: doc comments | ||
IEnumerable<CacheEntry> GetEntriesToEvict(IEnumerable<CacheEntry> entries, DateTimeOffset now); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be IList<CacheEntry>
? This structure worries me a little because it requires the caller to do a LINQ query (or some equivalent) by default. We should add a micro benchmark to see what the performance overhead of this is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea although this is the most generic collection, it may be a little too limited in terms of operations that are available on it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than returning a list, maybe just call a method / set a property on a given entry to mark it for eviction. Evict()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, SetExpired pretty much already does that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Concerned about the abstraction leaking, but I'll take another look when I have more screens available.
|
||
namespace Microsoft.Extensions.Caching.Memory | ||
{ | ||
public class DefaultMemoryCacheEvictionStrategy : IMemoryCacheEvictionStrategy |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid the word Default in type names
Any actionable changes to the API before I continue working on this @davidfowl @Tratcher ? |
@@ -9,7 +9,7 @@ | |||
|
|||
namespace Microsoft.Extensions.Caching.Memory | |||
{ | |||
internal class CacheEntry : ICacheEntry | |||
public class CacheEntry : ICacheEntry |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CacheEntry is the wrong view to expose to the eviction algorithm. This view is the one used for creating the entry so Value is settable, all the expirations are settable, AbsoluteExpirationRelativeToNow is relative to entry creation rather than the expiration scan, etc..
There should be a different interface/view exposed for creation vs eviction, even if the same object implements both internally.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll give this a try
public interface IMemoryCacheEvictionStrategy | ||
{ | ||
// TODO: doc comments | ||
IEnumerable<CacheEntry> GetEntriesToEvict(IEnumerable<CacheEntry> entries, DateTimeOffset now); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than returning a list, maybe just call a method / set a property on a given entry to mark it for eviction. Evict()
public interface IMemoryCacheEvictionStrategy | ||
{ | ||
// TODO: doc comments | ||
IEnumerable<CacheEntry> GetEntriesToEvict(IEnumerable<CacheEntry> entries, DateTimeOffset now); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, SetExpired pretty much already does that.
@@ -148,7 +136,6 @@ public static ICacheEntry SetOptions(this ICacheEntry entry, MemoryCacheEntryOpt | |||
entry.AbsoluteExpiration = options.AbsoluteExpiration; | |||
entry.AbsoluteExpirationRelativeToNow = options.AbsoluteExpirationRelativeToNow; | |||
entry.SlidingExpiration = options.SlidingExpiration; | |||
entry.Priority = options.Priority; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing Priority is unfortunate but I understand. Do we have another way to add custom metadata for the eviction algorithm without embedding it in the value itself? What if ICacheEntry had a transparent object EvictionMetadata
property?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about this since EvictionMetadata may not make sense when all components share the same memory cache in DI. Would each component then have to know what to set the EvictionMetadata to? Also, what is the overhead of embedding the information in the value itself over having this field (would need to test this in a microbenchmark).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All components share an eviction strategy anyways, and if the metadata were embedded in the value you'd have the same problem of everyone having to know about it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sample?
80931d4
to
a1b9702
Compare
|
||
private void TimerLoop(object state) | ||
{ | ||
if (EvictionCallback()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to have logic to prevent this from being re-entrant.
Think about the error handling for this new extensibility, especially on the timer thread. |
@@ -11,9 +11,9 @@ public class MemoryCacheOptions : IOptions<MemoryCacheOptions> | |||
{ | |||
public ISystemClock Clock { get; set; } | |||
|
|||
public bool CompactOnMemoryPressure { get; set; } = true; | |||
public IMemoryCacheEvictionStrategy EvictionStrategy { get; set; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are these options rather than DI injected?
public interface IMemoryCacheEvictionTrigger : IDisposable | ||
{ | ||
// TODO: doc comments | ||
Func<bool> EvictionCallback { get; set; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
void Initialize(Func<bool> evictionCallback);
or make it a parameter to Resume
{ | ||
return _isExpired || CheckForExpiredTime(now) || CheckForExpiredTokens(); | ||
} | ||
|
||
internal void SetExpired(EvictionReason reason) | ||
public void SetExpired(EvictionReason reason) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a little odd for this to be public. The only EvictionReason that would make sense for the eviction algorithm to set would be Capacity. Expired is the next best reason, but CheckExpired should set that. Maybe make a new method that's just Evict and always uses Capacity as the reason?
@@ -16,9 +13,9 @@ namespace Microsoft.Extensions.Caching.Memory | |||
/// An implementation of <see cref="IMemoryCache"/> using a dictionary to | |||
/// store its entries. | |||
/// </summary> | |||
public class MemoryCache : IMemoryCache | |||
public partial class MemoryCache : IMemoryCache |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
partial?
Closing this PR, we decided to remove the Gen2 GC trigger and allow users to downcast to use Compact on MemoryCache instead. |
#221 Need to review API changes and then continue with additional todos
TODO: