The following code gets or creates a cached item with absolute expiration: var cachedValue = _memor圜ache.GetOrCreate(ĬacheEntry.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(20) Ī cached item set with only a sliding expiration is at risk of never expiring.
The following code calls Get to fetch the cached time: var cacheEntry = _memor圜ache.Get(CacheKeys.Entry) Var cachedValue = await _memor圜ache.GetOrCreateAsync( Public async Task OnGetCacheGetOrCreateAsync() Var cachedValue = _memor圜ache.GetOrCreate(ĬacheEntry.SlidingExpiration = TimeSpan.FromSeconds(3) The following code uses GetOrCreate and GetOrCreateAsync to cache data. The cache entry gets evicted from the cache after one day, even if it's accessed within this timeout period. In the preceding code, the cache entry is configured with a relative expiration of one day. The current time and the cached time are displayed: Ĭurrent Time: Time: following code uses the Set extension method to cache data for a relative time without Memor圜acheEntryOptions: _memor圜ache.Set(CacheKeys.Entry, DateTime.Now, TimeSpan.FromDays(1)) The CacheKeys class is part of the download sample. Each time the cache entry is accessed, it remains in the cache for a further 3 seconds. If the cache entry isn't accessed for more than three seconds, it gets evicted from the cache. In the preceding code, the cache entry is configured with a sliding expiration of three seconds. _memor圜ache.Set(CacheKeys.Entry, cacheValue, cacheEntryOptions) SetSlidingExpiration(TimeSpan.FromSeconds(3)) Var cacheEntryOptions = new Memor圜acheEntryOptions() If (!_memor圜ache.TryGetValue(CacheKeys.Entry, out DateTime cacheValue)) If a time isn't cached, a new entry is created and added to the cache with Set: public void OnGet() The following code uses TryGetValue to check if a time is in the cache. Public IndexModel(IMemor圜ache memor圜ache) => Private readonly IMemor圜ache _memor圜ache Request the IMemor圜ache instance in the constructor: public class IndexModel : PageModel In-memory caching is a service that's referenced from an app using Dependency Injection. For more information and an example, see Use SetSize, Size, and SizeLimit to limit cache size.Ī shared cache is one shared by other frameworks or libraries. When using SetSize, Size, or SizeLimit to limit cache, create a cache singleton for caching. This can lead to issues since developers may not have full control on what uses the shared cache. When a size limit is set on a cache, all entries must specify a size when being added. Using a shared memory cache from Dependency Injection and calling SetSize, Size, or SizeLimit to limit cache size can cause the app to fail. It's up to the developer to limit cache size. The ASP.NET Core runtime does not limit cache size based on memory pressure. Use SetSize, Size, and SizeLimit to limit cache size.As an example, using arbitrary user-provided input as a cache key is not recommended since the input might consume an unpredictable amount of memory. Do not insert external input into the cache.The cache uses a scarce resource, memory.
For example, IMemor圜ache works natively with ASP.NET Core dependency injection. IMemor圜ache (described in this article) is recommended over / Memor圜ache because it's better integrated into ASP.NET Core. Memor圜ache ( NuGet package) can be used with: The in-memory and distributed cache store cache items as key-value pairs. The distributed cache interface is limited to byte. The in-memory cache can store any object. Using a distributed cache offloads the cache memory to an external process. For some apps, a distributed cache can support higher scale-out than an in-memory cache. Non-sticky sessions in a web farm require a distributed cache to avoid cache consistency problems. For example, Azure Web apps use Application Request Routing (ARR) to route all requests to the same server. Sticky sessions ensure that requests from a client all go to the same server. Apps running on a server farm (multiple servers) should ensure sessions are sticky when using the in-memory cache. IMemor圜ache represents a cache stored in the memory of the web server. The simplest cache is based on the IMemor圜ache. Apps should be written and tested to never depend on cached data.ĪSP.NET Core supports several different caches. Caching makes a copy of data that can be returned much faster than from the source. Caching works best with data that changes infrequently and is expensive to generate.
Caching can significantly improve the performance and scalability of an app by reducing the work required to generate content.