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

Add size based eviction to MemoryCache #332

Merged
merged 1 commit into from
Jul 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion Caching.sln
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26228.9
VisualStudioVersion = 15.0.26621.2
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Extensions.Caching.Memory", "src\Microsoft.Extensions.Caching.Memory\Microsoft.Extensions.Caching.Memory.csproj", "{966D16D8-5D4E-4433-9DA7-F53EE44B7EE7}"
EndProject
Expand Down Expand Up @@ -243,4 +243,7 @@ Global
{17E332EB-D18D-4BF5-BCA5-989E36C78B79} = {459E1593-2C11-42CB-AD17-F7597E69E5D2}
{ADF83AC7-3776-4E62-A222-C6979C32DD2D} = {9E78AA8E-7870-46DE-A49F-856F5A0A9166}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strange, not sure why vs added this.

SolutionGuid = {30FC4CB3-9789-4A49-939D-03398DBE03E0}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,24 @@ public static ICacheEntry SetValue(
return entry;
}

/// <summary>
/// Sets the size of the cache entry value.
/// </summary>
/// <param name="entry"></param>
/// <param name="size"></param>
public static ICacheEntry SetSize(
this ICacheEntry entry,
long size)
{
if (size < 0)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add some tests for these checks

{
throw new ArgumentOutOfRangeException(nameof(size), size, $"{nameof(size)} must be non-negative.");
}

entry.Size = size;
return entry;
}

/// <summary>
/// Applies the values of an existing <see cref="MemoryCacheEntryOptions"/> to the entry.
/// </summary>
Expand All @@ -149,6 +167,7 @@ public static ICacheEntry SetOptions(this ICacheEntry entry, MemoryCacheEntryOpt
entry.AbsoluteExpirationRelativeToNow = options.AbsoluteExpirationRelativeToNow;
entry.SlidingExpiration = options.SlidingExpiration;
entry.Priority = options.Priority;
entry.Size = options.Size;

foreach (var expirationToken in options.ExpirationTokens)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public enum EvictionReason
TokenExpired,

/// <summary>
/// GC, overflow
/// Overflow
/// </summary>
Capacity,
}
Expand Down
8 changes: 6 additions & 2 deletions src/Microsoft.Extensions.Caching.Abstractions/ICacheEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public interface ICacheEntry : IDisposable
/// Gets or sets an absolute expiration date for the cache entry.
/// </summary>
DateTimeOffset? AbsoluteExpiration { get; set; }

/// <summary>
/// Gets or sets an absolute expiration time, relative to now.
/// </summary>
Expand All @@ -50,9 +50,13 @@ public interface ICacheEntry : IDisposable

/// <summary>
/// Gets or sets the priority for keeping the cache entry in the cache during a
/// memory pressure triggered cleanup. The default is <see cref="CacheItemPriority.Normal"/>.
/// cleanup. The default is <see cref="CacheItemPriority.Normal"/>.
/// </summary>
CacheItemPriority Priority { get; set; }

/// <summary>
/// Gets or set the size of the cache entry value.
/// </summary>
long? Size { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@ public static MemoryCacheEntryOptions SetPriority(
return options;
}

/// <summary>
/// Sets the size of the cache entry value.
/// </summary>
/// <param name="options"></param>
/// <param name="size"></param>
public static MemoryCacheEntryOptions SetSize(
this MemoryCacheEntryOptions options,
long size)
{
if (size < 0)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does size==0 signify anything? Also, is this "Size in bytes"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want to impose what size means. When we use this in response caching, it will be bytes but in theory you can have a memory cache where all the sizes are 1 and in that case size means count.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Size of zero means you can add this entry even when the cache is full.

{
throw new ArgumentOutOfRangeException(nameof(size), size, $"{nameof(size)} must be non-negative.");
}

options.Size = size;
return options;
}

/// <summary>
/// Expire the cache entry if the given <see cref="IChangeToken"/> expires.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class MemoryCacheEntryOptions
private DateTimeOffset? _absoluteExpiration;
private TimeSpan? _absoluteExpirationRelativeToNow;
private TimeSpan? _slidingExpiration;
private long? _size;

/// <summary>
/// Gets or sets an absolute expiration date for the cache entry.
Expand Down Expand Up @@ -90,5 +91,22 @@ public TimeSpan? SlidingExpiration
/// memory pressure triggered cleanup. The default is <see cref="CacheItemPriority.Normal"/>.
/// </summary>
public CacheItemPriority Priority { get; set; } = CacheItemPriority.Normal;

/// <summary>
/// Gets or sets the size of the cache entry value.
/// </summary>
public long? Size
{
get => _size;
set
{
if (value < 0)
{
throw new ArgumentOutOfRangeException(nameof(value), value, $"{nameof(value)} must be non-negative.");
}

_size = value;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,15 @@
"TypeId": "public static class Microsoft.Extensions.Caching.Distributed.DistributedCacheExtensions",
"MemberId": "public static System.Threading.Tasks.Task<System.String> GetStringAsync(this Microsoft.Extensions.Caching.Distributed.IDistributedCache cache, System.String key)",
"Kind": "Removal"
},
{
"TypeId": "public interface Microsoft.Extensions.Caching.Memory.ICacheEntry : System.IDisposable",
"MemberId": "System.Nullable<System.Int64> get_Size()",
"Kind": "Addition"
},
{
"TypeId": "public interface Microsoft.Extensions.Caching.Memory.ICacheEntry : System.IDisposable",
"MemberId": "System.Void set_Size(System.Nullable<System.Int64> value)",
"Kind": "Addition"
}
]
18 changes: 18 additions & 0 deletions src/Microsoft.Extensions.Caching.Memory/CacheEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ internal class CacheEntry : ICacheEntry
internal DateTimeOffset? _absoluteExpiration;
internal TimeSpan? _absoluteExpirationRelativeToNow;
private TimeSpan? _slidingExpiration;
private long? _size;
private IDisposable _scope;

internal readonly object _lock = new object();
Expand Down Expand Up @@ -153,6 +154,23 @@ public IList<PostEvictionCallbackRegistration> PostEvictionCallbacks
/// </summary>
public CacheItemPriority Priority { get; set; } = CacheItemPriority.Normal;

/// <summary>
/// Gets or sets the size of the cache entry value.
/// </summary>
public long? Size
{
get => _size;
set
{
if (value < 0)
{
throw new ArgumentOutOfRangeException(nameof(value), value, $"{nameof(value)} must be non-negative.");
}

_size = value;
}
}

public object Key { get; private set; }

public object Value { get; set; }
Expand Down
Loading