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)

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

2.4 MemoryCacheEntryOptions

MemoryCacheEntryOptions is a memory cache configuration class through which cache-related strategies can be configured. 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 is 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 cache deletion strategy
 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. MemoryCache instances can optionally specify and enforce size limits. There is no defined unit of measurement for cache size limits because caches have no mechanism for measuring entry size. If a cache size limit is set, all entries must have a specified size. The ASP.NET Core runtime does not limit cache size based on memory pressure. Cache size is limited by the developer. The size specified is in the units selected by the developer.

For example:

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

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

What this means here is that cache size has no unit, we can set a total size and then set a size for each cache entry. Without setting a size, the cache may grow indefinitely 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, no matter how big it is here, it is set to 1, then there are up to 1024 cache items.
 memoryCacheEntryOption.SetSize(1);
 _cache.Set(CacheKey, cacheValue, memoryCacheEntryOption);
 

2.5 Cache Clean

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. 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 of expired items, which will be discussed below.

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

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

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

  • All due items.
  • Items arranged by priority. Remove the lowest priority items 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 priority NeverRemove are never removed. The function of the above code is to delete the cache entry for 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 cache items can also be controlled through CancellationChangeToken. Through it, the expiration policy of multiple cache objects can be controlled at the same time, so that related caches can expire at the same time to form a group. the concept of.

The following is 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 the 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, which is relative to the current absolute expiration time. If the time difference is passed in, the absolute expiration time will be calculated 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 cache deletion strategy
 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, no matter how big it is here, it is set to 1, then there are up to 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 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");
 }
 }
 }
 

Then 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, we can see that the first two cache acquisitions were normal. When the CancellationTokenSource.Cancel() method was called to cancel the request, the cache expired.

image

If CancellationTokenSource is used, this allows multiple cache entries to be evicted as a group. Using the using pattern in the above code, cache entries created within the using scope inherit the trigger and expiration settings. However, in this method, 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 are not included in the group.

image

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

_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 can find that the cached key is empty because the callback did not complete.
    • This may result in multiple threads repopulating cache entries.

  • When one 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 the parent entry is manually deleted or updated.

There are several other items in the official documentation, 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:

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

To put all deposited items into one group, you can also use the following methods:

_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 can find that the cached key is empty because the callback did not complete.
    • This may result in multiple threads repopulating cache entries.

  • When one 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 the parent entry is manually deleted or updated.

There are several other items in the official documentation, 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:

Table of Contents: ASP.NET Core Series Summary
Previous article: ASP.NET Core – Memory cache of cache (Part 1)
Next article: ASP.NET Core – Distributed cache of 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-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
首页
微信
电话
搜索