-
Notifications
You must be signed in to change notification settings - Fork 729
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'release/2.1' into release/2.2
- Loading branch information
Showing
116 changed files
with
13,973 additions
and
15 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<Project> | ||
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory)..\, Directory.Build.props))\Directory.Build.props" /> | ||
|
||
<PropertyGroup> | ||
<IsProductComponent>true</IsProductComponent> | ||
</PropertyGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using Microsoft.Extensions.Primitives; | ||
|
||
namespace Microsoft.Extensions.Caching.Memory | ||
{ | ||
public static class CacheEntryExtensions | ||
{ | ||
/// <summary> | ||
/// Sets the priority for keeping the cache entry in the cache during a memory pressure tokened cleanup. | ||
/// </summary> | ||
/// <param name="entry"></param> | ||
/// <param name="priority"></param> | ||
public static ICacheEntry SetPriority( | ||
this ICacheEntry entry, | ||
CacheItemPriority priority) | ||
{ | ||
entry.Priority = priority; | ||
return entry; | ||
} | ||
|
||
/// <summary> | ||
/// Expire the cache entry if the given <see cref="IChangeToken"/> expires. | ||
/// </summary> | ||
/// <param name="entry">The <see cref="ICacheEntry"/>.</param> | ||
/// <param name="expirationToken">The <see cref="IChangeToken"/> that causes the cache entry to expire.</param> | ||
public static ICacheEntry AddExpirationToken( | ||
this ICacheEntry entry, | ||
IChangeToken expirationToken) | ||
{ | ||
if (expirationToken == null) | ||
{ | ||
throw new ArgumentNullException(nameof(expirationToken)); | ||
} | ||
|
||
entry.ExpirationTokens.Add(expirationToken); | ||
return entry; | ||
} | ||
|
||
/// <summary> | ||
/// Sets an absolute expiration time, relative to now. | ||
/// </summary> | ||
/// <param name="entry"></param> | ||
/// <param name="relative"></param> | ||
public static ICacheEntry SetAbsoluteExpiration( | ||
this ICacheEntry entry, | ||
TimeSpan relative) | ||
{ | ||
entry.AbsoluteExpirationRelativeToNow = relative; | ||
return entry; | ||
} | ||
|
||
/// <summary> | ||
/// Sets an absolute expiration date for the cache entry. | ||
/// </summary> | ||
/// <param name="entry"></param> | ||
/// <param name="absolute"></param> | ||
public static ICacheEntry SetAbsoluteExpiration( | ||
this ICacheEntry entry, | ||
DateTimeOffset absolute) | ||
{ | ||
entry.AbsoluteExpiration = absolute; | ||
return entry; | ||
} | ||
|
||
/// <summary> | ||
/// Sets how long the cache entry can be inactive (e.g. not accessed) before it will be removed. | ||
/// This will not extend the entry lifetime beyond the absolute expiration (if set). | ||
/// </summary> | ||
/// <param name="entry"></param> | ||
/// <param name="offset"></param> | ||
public static ICacheEntry SetSlidingExpiration( | ||
this ICacheEntry entry, | ||
TimeSpan offset) | ||
{ | ||
entry.SlidingExpiration = offset; | ||
return entry; | ||
} | ||
|
||
/// <summary> | ||
/// The given callback will be fired after the cache entry is evicted from the cache. | ||
/// </summary> | ||
/// <param name="entry"></param> | ||
/// <param name="callback"></param> | ||
public static ICacheEntry RegisterPostEvictionCallback( | ||
this ICacheEntry entry, | ||
PostEvictionDelegate callback) | ||
{ | ||
if (callback == null) | ||
{ | ||
throw new ArgumentNullException(nameof(callback)); | ||
} | ||
|
||
return entry.RegisterPostEvictionCallback(callback, state: null); | ||
} | ||
|
||
/// <summary> | ||
/// The given callback will be fired after the cache entry is evicted from the cache. | ||
/// </summary> | ||
/// <param name="entry"></param> | ||
/// <param name="callback"></param> | ||
/// <param name="state"></param> | ||
public static ICacheEntry RegisterPostEvictionCallback( | ||
this ICacheEntry entry, | ||
PostEvictionDelegate callback, | ||
object state) | ||
{ | ||
if (callback == null) | ||
{ | ||
throw new ArgumentNullException(nameof(callback)); | ||
} | ||
|
||
entry.PostEvictionCallbacks.Add(new PostEvictionCallbackRegistration() | ||
{ | ||
EvictionCallback = callback, | ||
State = state | ||
}); | ||
return entry; | ||
} | ||
|
||
/// <summary> | ||
/// Sets the value of the cache entry. | ||
/// </summary> | ||
/// <param name="entry"></param> | ||
/// <param name="value"></param> | ||
public static ICacheEntry SetValue( | ||
this ICacheEntry entry, | ||
object value) | ||
{ | ||
entry.Value = value; | ||
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) | ||
{ | ||
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> | ||
/// <param name="entry"></param> | ||
/// <param name="options"></param> | ||
public static ICacheEntry SetOptions(this ICacheEntry entry, MemoryCacheEntryOptions options) | ||
{ | ||
if (options == null) | ||
{ | ||
throw new ArgumentNullException(nameof(options)); | ||
} | ||
|
||
entry.AbsoluteExpiration = options.AbsoluteExpiration; | ||
entry.AbsoluteExpirationRelativeToNow = options.AbsoluteExpirationRelativeToNow; | ||
entry.SlidingExpiration = options.SlidingExpiration; | ||
entry.Priority = options.Priority; | ||
entry.Size = options.Size; | ||
|
||
foreach (var expirationToken in options.ExpirationTokens) | ||
{ | ||
entry.AddExpirationToken(expirationToken); | ||
} | ||
|
||
foreach (var postEvictionCallback in options.PostEvictionCallbacks) | ||
{ | ||
entry.RegisterPostEvictionCallback(postEvictionCallback.EvictionCallback, postEvictionCallback.State); | ||
} | ||
|
||
return entry; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
namespace Microsoft.Extensions.Caching.Memory | ||
{ | ||
// TODO: Granularity? | ||
/// <summary> | ||
/// Specifies how items are prioritized for preservation during a memory pressure triggered cleanup. | ||
/// </summary> | ||
public enum CacheItemPriority | ||
{ | ||
Low, | ||
Normal, | ||
High, | ||
NeverRemove, | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/Caching/Abstractions/src/DistributedCacheEntryExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
|
||
namespace Microsoft.Extensions.Caching.Distributed | ||
{ | ||
public static class DistributedCacheEntryExtensions | ||
{ | ||
/// <summary> | ||
/// Sets an absolute expiration time, relative to now. | ||
/// </summary> | ||
/// <param name="options"></param> | ||
/// <param name="relative"></param> | ||
public static DistributedCacheEntryOptions SetAbsoluteExpiration( | ||
this DistributedCacheEntryOptions options, | ||
TimeSpan relative) | ||
{ | ||
options.AbsoluteExpirationRelativeToNow = relative; | ||
return options; | ||
} | ||
|
||
/// <summary> | ||
/// Sets an absolute expiration date for the cache entry. | ||
/// </summary> | ||
/// <param name="options"></param> | ||
/// <param name="absolute"></param> | ||
public static DistributedCacheEntryOptions SetAbsoluteExpiration( | ||
this DistributedCacheEntryOptions options, | ||
DateTimeOffset absolute) | ||
{ | ||
options.AbsoluteExpiration = absolute; | ||
return options; | ||
} | ||
|
||
/// <summary> | ||
/// Sets how long the cache entry can be inactive (e.g. not accessed) before it will be removed. | ||
/// This will not extend the entry lifetime beyond the absolute expiration (if set). | ||
/// </summary> | ||
/// <param name="options"></param> | ||
/// <param name="offset"></param> | ||
public static DistributedCacheEntryOptions SetSlidingExpiration( | ||
this DistributedCacheEntryOptions options, | ||
TimeSpan offset) | ||
{ | ||
options.SlidingExpiration = offset; | ||
return options; | ||
} | ||
} | ||
} |
Oops, something went wrong.