### Install Immediate.Cache using NuGet or .NET CLI Source: https://github.com/immediateplatform/immediate.cache/blob/master/readme.md Instructions for installing the Immediate.Cache library using either the NuGet Package Manager Console or the .NET Core command-line interface. ```CLI Install-Package Immediate.Cache ``` ```CLI dotnet add package Immediate.Cache ``` -------------------------------- ### Register Immediate.Cache services with Dependency Injection in C# Source: https://github.com/immediateplatform/immediate.cache/blob/master/readme.md Illustrates how to configure dependency injection for Immediate.Cache in `Program.cs`. This includes registering the in-memory cache, the `Owned<>` generic type, and the specific `GetValueCache` service as singletons. ```C# services.AddMemoryCache(); ``` ```C# services.AddSingleton(typeof(Owned<>)); ``` ```C# services.AddSingleton(); ``` -------------------------------- ### Retrieve cached data using Immediate.Cache in C# Source: https://github.com/immediateplatform/immediate.cache/blob/master/readme.md Demonstrates how to retrieve data from an instance of the custom cache class. If a value is cached, it's returned; otherwise, the handler is executed, and its result is stored in the cache. ```C# var response = await cache.GetValue(request, token); ``` -------------------------------- ### Create a custom cache class for Immediate.Handlers in C# Source: https://github.com/immediateplatform/immediate.cache/blob/master/readme.md Defines a `GetValue` handler and a `GetValueCache` class that extends `ApplicationCacheBase` to provide caching for the handler's responses. The `TransformKey` method is overridden to generate a unique cache key for each request. ```C# [Handler] public static partial class GetValue { public sealed record Query(int Value); public sealed record Response(int Value); private static ValueTask HandleAsync( Query query, CancellationToken _ ) => ValueTask.FromResult(new Response(query.Value)); } public sealed class GetValueCache( IMemoryCache memoryCache, Owned> ownedHandler ) : ApplicationCacheBase( memoryCache, ownedHandler ) { protected override string TransformKey(GetValue.Query request) => $"GetValue(query: {request.Value})"; } ``` -------------------------------- ### Update cached data using Immediate.Cache in C# Source: https://github.com/immediateplatform/immediate.cache/blob/master/readme.md Illustrates how to directly set or update a value in the cache. If a handler is currently running for the same request, it will be cancelled, and waiting callers will immediately receive the new response. ```C# await cache.SetValue(request, response); ``` -------------------------------- ### Remove cached data using Immediate.Cache in C# Source: https://github.com/immediateplatform/immediate.cache/blob/master/readme.md Shows how to invalidate and remove a specific entry from the cache. If a handler is currently processing the request, it will be cancelled, and any callers waiting on the results will experience a `CancellationToken` cancellation. ```C# await cache.RemoveValue(request); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.