1024programmer Asp.Net ASP.NET Core – Cache memory cache (Part 2)

ASP.NET Core – Cache memory cache (Part 2)

ASP.NET Core – Cache Memory Cache (Part 2)

Continued from the previous article ASP.NET Core – Cache Memory Cache (Part 1), so the directory here starts from 2.4.

2.4 MemoryCacheEntryOptions

MemoryCacheEntryOptions is a memory cache configuration class through which you can configure cache-related strategies. In addition to the expiration time mentioned above, we can also set the following:

  • Set cache priority.
  • Sets the PostEvictionDelegate that is called after an entry has been evicted from the cache.
    The callback runs on a different thread than the code that removes the item from the cache.
  • Limit cache size
var memoryCacheEntryOption = new MemoryCacheEntryOptions();
 memoryCacheEntryOption.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(3);
 // Cache priority, Low, Normal, High, NeverRemove, Normal is the default value, which is related to the strategy of cache deletion
 memoryCacheEntryOption. SetPriority(CacheItemPriority. Normal);
 // Register cache item deletion callback event
 memoryCacheEntryOption. RegisterPostEvictionCallback(PostEvictionDelegate);
 _cache.Set(CacheKey, cacheValue, memoryCacheEntryOption);

 public void PostEvictionDelegate(object cacheKey, object cacheValue, EvictionReason evictionReason, object state)
 {
 var memoryCache = (IMemoryCache)state;

 Console.WriteLine($"Entry {cacheKey}:{cacheValue} was evicted: {evictionReason}.");
 }
 

image

The cache size limit is used in conjunction with the configuration of the MemoryCache instance. A MemoryCache instance can optionally specify and enforce a size limit. There is no defined unit of measure for the cache size limit, because the cache has no mechanism for measuring the size of an entry. If a cache size limit is set, all entries must specify a size. The ASP.NET Core runtime does not limit the cache size based on memory pressure. Cache size is limited by the developer. The specified size is in the units chosen by the developer.

Example:

  • If the web application primarily caches strings, the size of each cache entry can be the string length.
  • Apps can specify a size of 1 for all entries, the size limit is the entry count.
    If SizeLimit is not set, the cache grows infinitely. The ASP.NET Core runtime does not prune the cache when the system is low on memory. Apps must be built as:

    • Limit cache growth.
    • Call Compact or Remove when available memory is limited.

This means that the cache size has no unit, we can set a total size, and then set a size for each cache entry. Without a set size, the cache may grow infinitely until it uses up all the memory on the server.

// We can set the cache size limit when registering the memory cache
 services.AddMemoryCache(options =>
 {
 options. SizeLimit = 1024;
 });

 // Then set the size of each cache item, which can be set according to the developer's judgment. For example, if it is set to 1 no matter how big it is, there are at most 1024 cache items
 memoryCacheEntryOption. SetSize(1);
 _cache.Set(CacheKey, cacheValue, memoryCacheEntryOption);
 

2.5 Cache Cleaner

The cache will not be automatically cleared in the background after it expires. There is no timer to actively scan the cache for expired items. Instead, any activity on the cache (Get, Set, Remove) can trigger a background scan for expired items. If CancellationTokenSource is used, its timer (CancelAfter) will also delete entries and trigger scanning for expired items, which will be discussed below.

In addition to triggering an expiration check on the corresponding cache item when operating on the cache, we can also clean up the cache by manually calling the Remove method and the Compact method.

_cache.Remove(cacheKey);
 _cache.Compact(.25);
 

The Compact method will attempt to delete the specified percentage of the cache in the following order:

  • All due items.
  • Items by priority. The lowest priority items are removed first.
  • The least recently used object.
  • The item with the shortest absolute expiration time.
  • The item with the shortest adjustable expiration time.

Pinned items with a priority of NeverRemove will never be removed. The function of the above code is to delete the cache entries for the cacheKey, and call Compact to delete 25% of the cache entries.

2.6 cache group

In addition to the expiration time setting mentioned above, the expiration policy of the cache item can also be controlled by CancellationChangeToken, through which the expiration policy of multiple cache objects can be controlled at the same time, and related caches can be expired at the same time, forming a group the concept of.

Here is the sample code:

First define two methods to separate cache setting and cache reading:

public interface ICacheService
 {
 public void PrintDateTimeNow();

 public void SetGroupDateTime();

 public void PrintGroupDateTime();
 }

 public class CacheService : ICacheService
 {
 public const string CacheKey = "CacheTime";
 public const string DependentCancellationTokenSourceCacheKey = "DependentCancellationTokenSource";
 public const string ParentCacheKey = "Parent";
 public const string ChildCacheKey = "Chlid";
 private readonly IMemoryCache _cache;
 public CacheService(IMemoryCache memoryCache)
 {
 _cache = memoryCache;
 }

 public void PrintDateTimeNow()
 {
 var time = DateTime. Now;
 if (!_cache. TryGetValue(CacheKey, out DateTime cacheValue))
 {
 cacheValue = time;
 // Set an absolute expiration time
 // The functions of the two implementations are the same, but the way of setting the time is different
 // What is passed in is AbsoluteExpirationRelativeToNow, relative to the current absolute expiration time, the incoming time difference will calculate the absolute expiration time based on the current time
 // _cache.Set(CacheKey, cacheValue, TimeSpan.FromSeconds(2));
 // What is passed in is AbsoluteExpiration, the absolute expiration time, a DateTimeOffset object is passed in, and the specific time needs to be clearly specified
 // _cache.Set(CacheKey, cacheValue, DateTimeOffset.Now.AddSeconds(2));

 //var memoryCacheEntryOption = new MemoryCacheEntryOptions();
 //// The sliding expiration time is a relative time
 //memoryCacheEntryOption.SlidingExpiration = TimeSpan.FromSeconds(3);
 //_cache.Set(CacheKey, cacheValue, memoryCacheEntryOption);

 var memoryCacheEntryOption = new MemoryCacheEntryOptions();
 memoryCacheEntryOption.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(3);
 // Cache priority, Low, Normal, High, NeverRemove, Normal is the default value, which is related to the strategy of cache deletion
 memoryCacheEntryOption. SetPriority(CacheItemPriority. Normal);
 memoryCacheEntryOption. RegisterPostEvictionCallback(PostEvictionDelegate);
 // Then set the size of each cache item, which can be set according to the developer's judgment. For example, set it to 1 no matter how big it is, and there will be a maximum of 1024 cache items
 memoryCacheEntryOption. SetSize(1);
 _cache.Set(CacheKey, cacheValue, memoryCacheEntryOption);
 }
 time = cacheValue;

 Console.WriteLine("Cache time: " + time.ToString("yyyy-MM-dd HH:mm:ss"));
 Console.WriteLine("Current time: " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
 }

 public void PostEvictionDelegate(object cacheKey, object cacheValue, EvictionReason evictionReason, object state)
 {
 var memoryCache = (IMemoryCache)state;

 Console.WriteLine($"Entry {cacheKey}:{cacheValue} was evicted: {evictionReason}.");
 }

 public void SetGroupDateTime()
 {
 // Here is to save the CancellationTokenSource so that it can be obtained externally
 var cancellationTokenSource = new CancellationTokenSource();
 _cache. Set(
 DependentCancellationTokenSourceCacheKey,
 cancellationTokenSource);

 using var parentCacheEntry = _cache. CreateEntry(ParentCacheKey);

 parentCacheEntry.Value = DateTime.Now;

 Task.Delay(TimeSpan.FromSeconds(1)).Wait();

 _cache. Set(
 ChildCacheKey,
 DateTime. Now,
 new CancellationChangeToken(cancellationTokenSource.Token));
 }

 public void PrintGroupDateTime()
 {
 if(_cache.TryGetValue(ParentCacheKey, out DateTime parentCacheDateTime))
 {
 Console.WriteLine("ParentDateTime:" + parentCacheDateTime.ToString("yyyy-MM-dd HH:mm:ss"));
 }
 else
 {
 Console.WriteLine("ParentDateTime is canceled");
 }

 if (_cache. TryGetValue(ChildCacheKey, out DateTime childCacheDateTime))
 {
 Console.WriteLine("ChildDateTime:" + childCacheDateTime.ToString("yyyy-MM-dd HH:mm:ss"));
 }
 else
 {
 Console.WriteLine("ChildDateTime is canceled");
 }
 }
 }
 

After that, modify the test code in the entry file:

var service = host.Services.GetRequiredService();
 service. SetGroupDateTime();

 service.PrintGroupDateTime();

 service.PrintGroupDateTime();

 var cache = host.Services.GetRequiredService();
 var cancellationTokenSource = cache.Get(CacheService.DependentCancellationTokenSourceCacheKey);
 cancellationTokenSource.Cancel();

 service.PrintGroupDateTime();
 

From the console output, it can be seen that the first two caches were obtained normally, and when the CancellationTokenSource.Cancel() method was called to cancel the request, the cache expired.

image

If using CancellationTokenSource, allows multiple cache entries to be evicted as a group. With the using pattern in the above code, cache entries created within the using scope inherit the trigger and expiration settings. However, in this way, only the cache items in the using scope and the cache items using CancellationTokenSource in the using scope can form a group. If there are other cache items in the scope, they will not be included in a group.

image

If you want to include multiple cache items into one group, you can also use the following method:

_cache.Set(ParentCacheKey,
 DateTime. Now,
 new CancellationChangeToken(cancellationTokenSource.Token));

 _cache. Set(
 ChildCacheKey,
 DateTime. Now,
 new CancellationChangeToken(cancellationTokenSource.Token));

 _cache. Set(
 ChildsCacheKey,
 DateTime. Now,
 new CancellationChangeToken(cancellationTokenSource.Token));
 

image

2.7 Some Notes

  • When using callbacks to repopulate cache items:

    • Multiple requests may find the cached key to be empty because the callback did not complete.
    • This may cause multiple threads to repopulate cache entries.

  • When a cache entry is used to create another cache entry, the child entry copies the parent entry’s expiration token and time-based expiration settings. Child entries do not expire when parent entries are manually deleted or updated.

There are several other items in the official document, but I have already mentioned them above, so I won’t repeat them here.

Reference article:
In-memory caching in ASP.NET Core

ASP.NET Core Series:

Contents: Summary of ASP.NET Core Series
Previous: ASP.NET Core – Cache Memory Cache (Part 1)
Next article: ASP.NET Core – Distributed Cache for Cache

All saved items can be included in a group, and the following methods can also be used:

_cache.Set(ParentCacheKey,
 DateTime. Now,
 new CancellationChangeToken(cancellationTokenSource.Token));

 _cache. Set(
 ChildCacheKey,
 DateTime. Now,
 new CancellationChangeToken(cancellationTokenSource.Token));

 _cache. Set(
 ChildsCacheKey,
 DateTime. Now,
 new CancellationChangeToken(cancellationTokenSource.Token));
 

image

2.7 Some Notes

  • When using callbacks to repopulate cache items:

    • Multiple requests may find the cached key to be empty because the callback did not complete.
    • This may cause multiple threads to repopulate cache entries.

  • When a cache entry is used to create another cache entry, the child entry copies the parent entry’s expiration token and time-based expiration settings. Child entries do not expire when parent entries are manually deleted or updated.

There are several other items in the official document, but I have already mentioned them above, so I won’t repeat them here.

Reference article:
In-memory caching in ASP.NET Core

ASP.NET Core Series:

Contents: Summary of ASP.NET Core Series
Previous: ASP.NET Core – Cache Memory Cache (Part 1)
Next article: ASP.NET Core – Distributed Cache for Cache

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/asp-net-core-cache-memory-cache-part-2/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索