mirror of
https://github.com/m-lamonaca/dev-notes.git
synced 2025-04-08 11:56:41 +00:00
minimal api: add output cache notes
This commit is contained in:
parent
a3de118642
commit
29ed3dab2b
1 changed files with 61 additions and 0 deletions
|
@ -263,3 +263,64 @@ In fact, the `user-jwts` tool utilizes the `user-secrets` infrastructure to mana
|
||||||
```sh
|
```sh
|
||||||
dotnet user-jwts create # configure a dev JWT fot the current user
|
dotnet user-jwts create # configure a dev JWT fot the current user
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Output Caching
|
||||||
|
|
||||||
|
```cs
|
||||||
|
builder.Services.AddOutputCaching(); // no special options
|
||||||
|
builder.Services.AddOutputCaching(options =>
|
||||||
|
{
|
||||||
|
options => options.AddBasePolicy(x => x.NoCache()) // no cache policy
|
||||||
|
|
||||||
|
Func<OutputCacheContext, bool> predicate = /* discriminate requests */
|
||||||
|
options.AddBasePolicy(x => x.With(predicate).CachePolicy());
|
||||||
|
options.AddBasePolicy("<policy-name>", x => x.CachePolicy()); // named policy
|
||||||
|
});
|
||||||
|
|
||||||
|
// [...]
|
||||||
|
|
||||||
|
app.UseOutputCaching(); // following middlewares can use output cache
|
||||||
|
|
||||||
|
// [...]
|
||||||
|
|
||||||
|
app.MapGet("/<route>", RouteHandler).CacheOutput(); // cache forever
|
||||||
|
app.MapGet("/<route>", RouteHandler).CacheOutput().Expire(timespan);
|
||||||
|
|
||||||
|
app.MapGet("/<route>", RouteHandler).CacheOutput(x => x.CachePolicy());
|
||||||
|
app.MapGet("/<route>", RouteHandler).CacheOutput("<policy-name>");
|
||||||
|
|
||||||
|
app.MapGet("/<route>", RouteHandler).CacheOutput(x => x.VaryByHeader(/* headers list */));
|
||||||
|
app.MapGet("/<route>", RouteHandler).CacheOutput(x => x.VaryByQuery(/* query key */));
|
||||||
|
app.MapGet("/<route>", RouteHandler).CacheOutput(x => x.VaryByValue());
|
||||||
|
|
||||||
|
app.MapGet("/<route>", [OutputCache(/* options */)]RouteHandler);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Cache Eviction
|
||||||
|
|
||||||
|
```cs
|
||||||
|
|
||||||
|
app.MapGet("/<route-one>", RouteHandler).CacheOutput(x => x.Tag("<tag>")); // tag cache portion
|
||||||
|
|
||||||
|
app.MapGet("/<route-two>", (IOutputCacheStore cache, CancellationToken token) =>
|
||||||
|
{
|
||||||
|
await cache.EvictByTag("<tag>", token); // invalidate a portion of the cache
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### Custom Cache Policy
|
||||||
|
|
||||||
|
```cs
|
||||||
|
app.MapGet("/<route-one>", RouteHandler).CacheOutput(x => x.AddCachePolicy<CustomCachePolicy>());
|
||||||
|
```
|
||||||
|
|
||||||
|
```cs
|
||||||
|
class CustomCachePolicy : IOutputCachePolicy
|
||||||
|
{
|
||||||
|
public ValueTask CacheRequestAsync(OutputCacheContext context, CancellationToken cancellationToken) { }
|
||||||
|
|
||||||
|
public ValueTask ServeFromCacheAsync(OutputCacheContext context, CancellationToken cancellationToken) { }
|
||||||
|
|
||||||
|
public ValueTask ServeResponseAsync(OutputCacheContext context, CancellationToken cancellationToken) { }
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
Loading…
Add table
Reference in a new issue