此操作将删除页面 "Cache In-memory In ASP.Internet Core"
,请三思而后行。
Caching can significantly improve the efficiency and scalability of an app by lowering the work required to generate content material. Caching works greatest with information that modifications infrequently and is expensive to generate. Caching makes a copy of knowledge that may be returned a lot sooner than from the source. Apps ought to be written and tested to by no means depend upon cached data. The best cache is predicated on the IMemoryCache. IMemoryCache represents a cache saved in the Memory Wave of the online server. Apps running on a server farm (multiple servers) ought to guarantee classes are sticky when using the in-memory cache. Sticky sessions be sure that requests from a consumer all go to the identical server. For instance, 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 keep away from cache consistency problems. For Memory Wave Protocol some apps, a distributed cache can help larger scale-out than an in-memory cache.
Using a distributed cache offloads the cache memory to an exterior course of. The in-Memory Wave Protocol cache can store any object. The in-Memory Wave and distributed cache retailer cache items as key-worth pairs. Internet Normal 2.Zero or later. Any .Net implementation that targets .Internet Customary 2.Zero or later. Internet Framework 4.5 or later. Code ought to always have a fallback choice to fetch knowledge and not rely on a cached value being obtainable. The cache uses a scarce resource, memory. Restrict cache progress: - Don't insert exterior input into the cache. For instance, using arbitrary person-supplied input as a cache key shouldn't be advisable for the reason that enter might eat an unpredictable quantity of memory. Use expirations to limit cache growth. Use SetSize, Size, and SizeLimit to restrict cache measurement. It's as much as the developer to restrict cache dimension. Utilizing a shared memory cache from Dependency Injection and calling SetSize, Dimension, or SizeLimit to limit cache dimension can cause the app to fail.
When a measurement restrict is about on a cache, all entries must specify a dimension when being added. This could lead to points since builders may not have full control on what makes use of the shared cache. When using SetSize, Measurement, or SizeLimit to limit cache, create a cache singleton for caching. For more information and an example, see Use SetSize, Size, and SizeLimit to restrict cache dimension. A shared cache is one shared by different frameworks or libraries. In-memory caching is a service that's referenced from an app using Dependency Injection. The next code makes use of TryGetValue to verify if a time is within the cache. In the previous 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 gets evicted from the cache. Each time the cache entry is accessed, it stays in the cache for an extra three seconds.
The CacheKeys class is part of the download sample. Within the preceding code, the cache entry is configured with a relative expiration of someday. The cache entry will get evicted from the cache after in the future, even when it's accessed within this timeout interval. The following code uses GetOrCreate and GetOrCreateAsync to cache information. A cached item set with solely a sliding expiration is vulnerable to never expiring. If the cached item is repeatedly accessed inside the sliding expiration interval, the merchandise never expires. Combine a sliding expiration with an absolute expiration to guarantee the merchandise expires. Absolutely the expiration sets an upper sure on how lengthy the item might be cached while still permitting the merchandise to expire earlier if it isn't requested within the sliding expiration interval. If either the sliding expiration interval or absolutely the expiration time move, the merchandise is evicted from the cache. The preceding code ensures the data will not be cached longer than the absolute time.
GetOrCreate, GetOrCreateAsync, and Get are extension methods within the CacheExtensions class. These methods prolong the potential of IMemoryCache. Units the cache priority to CacheItemPriority.NeverRemove. Units a PostEvictionDelegate that will get referred to as after the entry is evicted from the cache. The callback is run on a distinct thread from the code that removes the item from the cache. A MemoryCache instance might optionally specify and implement a size restrict. The cache dimension restrict doesn't have a defined unit of measure as a result of the cache has no mechanism to measure the scale of entries. If the cache size limit is ready, all entries should specify size. It is as much as the developer to restrict cache measurement. The scale specified is in models the developer chooses. If the net app was primarily caching strings, each cache entry dimension may very well be the string length. The app may specify the size of all entries as 1, and the scale limit is the depend of entries. If SizeLimit is not set, the cache grows with out bound.
此操作将删除页面 "Cache In-memory In ASP.Internet Core"
,请三思而后行。