|
| 1 | +--- |
| 2 | +title: Creating a cache |
| 3 | +--- |
| 4 | + |
| 5 | +# {$frontmatter.title} |
| 6 | + |
| 7 | +Create a subclass of `ApplicationCacheBase`, which will serve as the cache for a particular handler. An example: |
| 8 | +```cs |
| 9 | +[Handler] |
| 10 | +public static partial class GetValue |
| 11 | +{ |
| 12 | + public sealed record Query(int Value); |
| 13 | + public sealed record Response(int Value); |
| 14 | + |
| 15 | + private static ValueTask<Response> HandleAsync( |
| 16 | + Query query, |
| 17 | + CancellationToken _ |
| 18 | + ) => ValueTask.FromResult(new Response(query.Value)); |
| 19 | +} |
| 20 | + |
| 21 | +public sealed class GetValueCache( |
| 22 | + IMemoryCache memoryCache, |
| 23 | + Owned<IHandler<GetValue.Query, GetValue.Response>> ownedHandler |
| 24 | +) : ApplicationCacheBase<GetValue.Query, GetValue.Response>( |
| 25 | + memoryCache, |
| 26 | + ownedHandler |
| 27 | +) |
| 28 | +{ |
| 29 | + protected override string TransformKey(GetValue.Query request) => |
| 30 | + $"GetValue(query: {request.Value})"; |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +In this case, the `GetValueCache` class will serve as a cache for the `GetValue` IH handler. |
| 35 | + |
| 36 | +## Register the Cache with DI |
| 37 | + |
| 38 | +In your `Program.cs` file: |
| 39 | + |
| 40 | +* Ensure that Memory Cache is registered, by calling: |
| 41 | +```cs |
| 42 | +services.AddMemoryCache(); |
| 43 | +``` |
| 44 | + |
| 45 | +* Register `Owned<>` as a singleton |
| 46 | +```cs |
| 47 | +services.AddSingleton(typeof(Owned<>)); |
| 48 | +``` |
| 49 | + |
| 50 | +* Register your cache service(s) as a singleton(s) |
| 51 | +```cs |
| 52 | +services.AddSingleton<GetValueCache>(); |
| 53 | +``` |
| 54 | + |
| 55 | +## Retrieve Data From the Cache |
| 56 | + |
| 57 | +Using an instance of the `GetValueCache` class that you have created above, you can simply call: |
| 58 | +```cs |
| 59 | +var response = await cache.GetValue(request, token); |
| 60 | +``` |
| 61 | + |
| 62 | +If there is a cached value, it will be returned; otherwise a temporary scope will be used to create the handler and |
| 63 | +execute it; and the returned value will be stored. |
| 64 | + |
| 65 | +> [!NOTE] |
| 66 | +> If simultaneous requests are made while the handler is executing, they will wait for the first handler to |
| 67 | +complete, rather than executing the handler a second/simultaenous time. |
| 68 | + |
| 69 | +## Removing Data From the Cache |
| 70 | + |
| 71 | +Using an instance of the `GetValueCache` class that you have created above, you can remove cached data by calling: |
| 72 | +```cs |
| 73 | +await cache.RemoveValue(request); |
| 74 | +``` |
| 75 | + |
| 76 | +> [!NOTE] |
| 77 | +> If a handler is running based on this request, it will be cancelled, and any callers waiting on the results from |
| 78 | +> this handler will experience a `CancellationToken` cancellation. |
| 79 | +
|
| 80 | +## Updating Data In the Cache |
| 81 | + |
| 82 | +Using an instance of the `GetValueCache` class that you have created above, you can assign cached data by calling: |
| 83 | +```cs |
| 84 | +await cache.SetValue(request, response); |
| 85 | +``` |
| 86 | + |
| 87 | +> [!NOTE] |
| 88 | +> If a handler is running based on this request, it will be cancelled, and any callers waiting on the results from |
| 89 | +> this handler will immediately receive the updated response. |
0 commit comments