Alle Redis Client Managers implementerer både IRedisClientsManager
og IRedisClientsManagerAsync
så IOC-registreringer forbliver de samme, som kan fortsætte med at registrere sig mod den eksisterende IRedisClientsManager
grænseflade, f.eks.:
container.Register<IRedisClientsManager>(c =>
new RedisManagerPool(redisConnectionString));
Hvor det kan bruges til at løse både sync IRedisClient
og async IRedisClientAsync
klienter, f.eks.:
using var syncRedis = container.Resolve<IRedisClientsManager>().GetClient();
await using var asyncRedis = await container.Resolve<IRedisClientsManager>().GetClientAsync();
Hvis du vil tvinge API-brug til kun async, kan du vælge blot at registrere IRedisClientsManagerAsync
hvor det kun lader dig løse async kun IRedisClientAsync
og ICacheClientAsync
klienter, f.eks.:
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IRedisClientsManagerAsync>(c => new RedisManagerPool());
}
//...
public class MyDep
{
private IRedisClientsManagerAsync manager;
public MyDep(IRedisClientsManagerAsync manager) => this.manager = manager;
public async Task<long> Incr(string key, uint value)
{
await using var redis = await manager.GetClientAsync();
return await redis.IncrementAsync(key, value);
}
}
Brug i ServiceStack #
Inde i ServiceStack Services &Controllere anbefaler vi at bruge GetRedisAsync()
for at løse en IRedisClientAsync
:
public class MyService : Service
{
public async Task<object> Any(MyRequest request)
{
await using var redis = await GetRedisAsync();
await redis.IncrementAsync(nameof(MyRequest), 1);
}
}
public class HomeController : ServiceStackController
{
public async Task<ActionResult> Index()
{
await using var redis = await GetRedisAsync();
await redis.IncrementAsync(nameof(HomeController), 1);
}
}