Skip to content

Commit 9618cf7

Browse files
Update documentation
1 parent e7977a9 commit 9618cf7

File tree

1 file changed

+87
-1
lines changed

1 file changed

+87
-1
lines changed

readme.md

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
[![GitHub Actions](https://github.com/ImmediatePlatform/Immediate.Cache/actions/workflows/build.yml/badge.svg)](https://github.com/ImmediatePlatform/Immediate.Cache/actions)
99
---
1010

11-
Immediate.Cache is...
11+
Immediate.Cache is a collection of classes that simplify caching responses from [Immediate.Handlers](https://github.com/ImmediatePlatform/Immediate.Handlers) handlers.
1212

1313
## Installing Immediate.Cache
1414

@@ -23,3 +23,89 @@ Or via the .NET Core command line interface:
2323
Either commands, from Package Manager Console or .NET Core CLI, will download and install Immediate.Cache.
2424

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

0 commit comments

Comments
 (0)