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

MemoryCache eviction restructure #280

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add EvictionMetadata
  • Loading branch information
JunTaoLuo committed Mar 16, 2017
commit 4ec49dc9f639c44ef8806d2aa30e0be29a8afba9
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ public static ICacheEntry SetOptions(this ICacheEntry entry, MemoryCacheEntryOpt
entry.AbsoluteExpiration = options.AbsoluteExpiration;
entry.AbsoluteExpirationRelativeToNow = options.AbsoluteExpirationRelativeToNow;
entry.SlidingExpiration = options.SlidingExpiration;
entry.EvictionMetadata = options.EvictionMetadata;

foreach (var expirationToken in options.ExpirationTokens)
{
Expand Down
3 changes: 2 additions & 1 deletion src/Microsoft.Extensions.Caching.Abstractions/ICacheEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public interface ICacheEntry : IDisposable
/// </summary>
TimeSpan? SlidingExpiration { get; set; }

object EvictionMetadata { get; set; }

/// <summary>
/// Gets the <see cref="IChangeToken"/> instances which cause the cache entry to expire.
/// </summary>
Expand All @@ -47,6 +49,5 @@ public interface ICacheEntry : IDisposable
/// Gets or sets the callbacks will be fired after the cache entry is evicted from the cache.
/// </summary>
IList<PostEvictionCallbackRegistration> PostEvictionCallbacks { get; }

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ public static MemoryCacheEntryOptions SetSlidingExpiration(
return options;
}

public static MemoryCacheEntryOptions SetEvictionMetadata(
this MemoryCacheEntryOptions options,
object metadata)
{
options.EvictionMetadata = metadata;
return options;
}

/// <summary>
/// The given callback will be fired after the cache entry is evicted from the cache.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public TimeSpan? SlidingExpiration
}
}

public object EvictionMetadata { get; set; }

/// <summary>
/// Gets the <see cref="IChangeToken"/> instances which cause the cache entry to expire.
/// </summary>
Expand Down
4 changes: 3 additions & 1 deletion src/Microsoft.Extensions.Caching.Memory/CacheEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Microsoft.Extensions.Caching.Memory
{
public class CacheEntry : ICacheEntry, IRetrievedCacheEntry
internal class CacheEntry : ICacheEntry, IRetrievedCacheEntry
{
private bool _added = false;
private static readonly Action<object> ExpirationCallback = ExpirationTokensExpired;
Expand Down Expand Up @@ -157,6 +157,8 @@ public IList<PostEvictionCallbackRegistration> PostEvictionCallbacks

internal EvictionReason EvictionReason { get; private set; }

public object EvictionMetadata { get; set; }

public void Dispose()
{
if (!_added)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public interface IRetrievedCacheEntry
/// </summary>
TimeSpan? SlidingExpiration { get; }

object EvictionMetadata { get; }

DateTimeOffset LastAccessed { get; }

bool IsExpired { get; }
Expand Down
3 changes: 2 additions & 1 deletion src/Microsoft.Extensions.Caching.Memory/MemoryCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -249,7 +250,7 @@ private bool ExecuteCacheEviction()
{
var evictedEntries = false;
var utcNow = _clock.UtcNow;
var freshEntries = new List<CacheEntry>();
var freshEntries = new List<IRetrievedCacheEntry>();

// TODO: evaluate the perf overhead of enumerators vs taking a snapshot
foreach (var entry in _entries)
Expand Down