-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDistributedCache.Example.cs
More file actions
39 lines (34 loc) · 1.45 KB
/
DistributedCache.Example.cs
File metadata and controls
39 lines (34 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using CodeCargo.Nats.DistributedCache;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using NATS.Client.Core;
using NATS.Client.KeyValueStore;
using NATS.Extensions.Microsoft.DependencyInjection;
using NATS.Net;
// Example used in the README.md "Use `IDistributedCache` Directly" section
public static class DistributedCacheExample
{
public static async Task Run(string[] args)
{
// Set the NATS URL, this normally comes from configuration
const string natsUrl = "nats://localhost:4222";
// Create a host builder for a Console application
// For a Web Application you can use WebApplication.CreateBuilder(args)
var builder = Host.CreateDefaultBuilder(args);
builder.ConfigureServices(services =>
{
services.AddNatsClient(natsBuilder => natsBuilder.ConfigureOptions(opts => opts with { Url = natsUrl }));
services.AddNatsDistributedCache(options =>
{
options.BucketName = "cache";
});
});
var host = builder.Build();
// Ensure that the KV Store is created
var natsConnection = host.Services.GetRequiredService<INatsConnection>();
var kvContext = natsConnection.CreateKeyValueStoreContext();
await kvContext.CreateOrUpdateStoreAsync(new NatsKVConfig("cache") { LimitMarkerTTL = TimeSpan.FromSeconds(1) });
// Start the host
await host.RunAsync();
}
}