すべてのRedisクライアントマネージャーは、両方のIRedisClientsManager
を実装します およびIRedisClientsManagerAsync
したがって、IOC登録は同じままであり、既存のIRedisClientsManager
に対して引き続き登録できます。 インターフェース、例:
container.Register<IRedisClientsManager>(c =>
new RedisManagerPool(redisConnectionString));
両方の同期IRedisClient
を解決するために使用できる場所 および非同期IRedisClientAsync
クライアント、例:
using var syncRedis = container.Resolve<IRedisClientsManager>().GetClient();
await using var asyncRedis = await container.Resolve<IRedisClientsManager>().GetClientAsync();
非同期のみを強制する場合は、APIを使用してIRedisClientsManagerAsync
を登録することを選択できます。 非同期のみを解決できる場合IRedisClientAsync
およびICacheClientAsync
クライアント、例:
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);
}
}
ServiceStackでの使用#
ServiceStack Services&Controllers内では、GetRedisAsync()
を使用することをお勧めします 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);
}
}