Nomirun SDK contains IDistributedCache
(from Microsoft.Extensions.Caching.Abstractions
) implementation for Etcd.
Register Etcd cache:
services.AddEtcdCache(options =>
{
options.ConnectionString = Configuration["Etcd:ConnectionString"];
options.Username = Configuration["Etcd:Username"];
options.Password = Configuration["Etcd:Password"];
});
This will register Etcd for distributed cache as a singleton.
Then you can use it in your code by injecting it to the constructor:
public class MyProcess
{
private readonly IDistributedCache _cache;
public MyProcess(IDistributedCache cache)
{
_cache = cache;
}
public async int Calc()
{
var sum = _calculator.Sum(43, 342);
await _cache.SetAsync("key", Encoding.UTF8.GetBytes(sum.ToString()));
return sum;
}
}
Whole IDistributedCache
interface is implemented and you can use:
public interface IDistributedCache
{
byte[]? Get(string key);
Task<byte[]?> GetAsync(string key, CancellationToken token = default (CancellationToken));
void Set(string key, byte[] value, DistributedCacheEntryOptions options);
Task SetAsync(
string key,
byte[] value,
DistributedCacheEntryOptions options,
CancellationToken token = default (CancellationToken));
void Refresh(string key);
Task RefreshAsync(string key, CancellationToken token = default (CancellationToken));
void Remove(string key);
Task RemoveAsync(string key, CancellationToken token = default (CancellationToken));
}