Add Cache In-memory In ASP.Internet Core

master
Moises Baracchi 2025-09-22 12:59:36 +08:00
parent ab6340fd62
commit a708861830
1 changed files with 9 additions and 0 deletions

@ -0,0 +1,9 @@
<br>Caching can considerably improve the performance and scalability of an app by lowering the work required to generate content. Caching works finest with knowledge that changes infrequently and is costly to generate. Caching makes a copy of data that may be returned a lot quicker than from the source. Apps should be written and examined to never depend on cached information. The simplest cache relies on the IMemoryCache. IMemoryCache represents a cache saved within the [Memory Wave System](http://pasarinko.zeroweb.kr/bbs/board.php?bo_table=notice&wr_id=7077300) of the web server. Apps running on a server farm (a number of servers) should ensure sessions are sticky when using the in-memory cache. Sticky periods ensure that requests from a client all go to the identical server. For example, Azure Net apps use Software Request Routing (ARR) to route all requests to the same server. Non-sticky periods in an internet farm require a distributed cache to avoid cache consistency problems. For some apps, a distributed cache can assist greater scale-out than an in-memory cache.<br>
<br>Using a distributed cache offloads the cache memory to an external course of. The in-memory cache can retailer any object. The in-memory and distributed cache retailer cache gadgets as key-value pairs. Net Commonplace 2.Zero or later. Any .Internet implementation that targets .Internet Normal 2.Zero or later. Net Framework 4.5 or later. Code should all the time have a fallback option to fetch knowledge and never depend upon a cached worth being accessible. The cache makes use of a scarce useful resource, memory. Limit cache progress: - Do not insert external enter into the cache. For example, utilizing arbitrary user-supplied enter as a cache key isn't beneficial since the input might eat an unpredictable quantity of memory. Use expirations to limit cache development. Use SetSize, Size, and SizeLimit to restrict cache size. It is up to the developer to restrict cache dimension. Utilizing a shared memory cache from Dependency Injection and calling SetSize, [Memory Wave System](https://tailandiatours.com/buceo-tailandia/) Measurement, or SizeLimit to restrict cache size could cause the app to fail.<br>
<br>When a size limit is about on a cache, all entries must specify a measurement when being added. This can lead to issues since builders might not have full management on what uses the shared cache. When using SetSize, Measurement, or SizeLimit to restrict cache, create a cache singleton for caching. For more data and an instance, see Use SetSize, Size, and SizeLimit to restrict cache size. A shared cache is one shared by other frameworks or [libraries](https://www.vocabulary.com/dictionary/libraries). In-memory caching is a service that's referenced from an app utilizing Dependency Injection. The next code uses TryGetValue to check if a time is in the cache. Within the preceding code, the cache entry is configured with a sliding expiration of three seconds. If the cache entry is not accessed for greater than three seconds, it will get evicted from the cache. Every time the cache entry is accessed, it remains in the cache for a further three seconds.<br>
<br>The CacheKeys class is a part of the obtain pattern. In the preceding code, the cache entry is configured with a relative expiration of sooner or later. The [cache entry](https://www.blogher.com/?s=cache%20entry) will get evicted from the cache after at some point, even when it is accessed within this timeout interval. The next code makes use of GetOrCreate and GetOrCreateAsync to cache knowledge. A cached merchandise set with only a sliding expiration is liable to never expiring. If the cached item is repeatedly accessed throughout the sliding expiration interval, the merchandise by no means expires. Mix a sliding expiration with an absolute expiration to ensure the merchandise expires. The absolute expiration units an upper sure on how lengthy the merchandise may be cached while nonetheless allowing the merchandise to expire earlier if it is not requested throughout the sliding expiration interval. If either the sliding expiration interval or absolutely the expiration time pass, the item is evicted from the cache. The preceding code ensures the information won't be cached longer than the absolute time.<br>
<br>GetOrCreate, GetOrCreateAsync, and Get are extension methods within the CacheExtensions class. These strategies prolong the capability of IMemoryCache. Sets the cache priority to CacheItemPriority.NeverRemove. Units a PostEvictionDelegate that will get known as after the entry is evicted from the cache. The callback is run on a different thread from the code that removes the item from the cache. A MemoryCache instance might optionally specify and implement a size limit. The cache size restrict would not have a defined unit of measure as a result of the cache has no mechanism to measure the scale of entries. If the cache dimension restrict is set, all entries must specify size. It is up to the developer to limit cache dimension. The size specified is in units the developer chooses. If the online app was primarily caching strings, each cache entry size might be the string length. The app may specify the size of all entries as 1, and the scale limit is the count of entries. If SizeLimit isn't set, the cache grows with out sure.<br>