Reputation: 12557
I connect to redis in a cluster with the following code. I would use it in a webapi project with a lot of traffic and I'm concerned about the missing async support.
Does anyone have some experiences with this ? I also dindn't find an offical support email adress for this.
var sentinel = new RedisSentinel(sentinels, "mymaster");
var redisManager = sentinel.Start();
while (true)
{
Console.WriteLine("Key add: ");
string key = Console.ReadLine();
using (var redis = redisManager.GetClient())
{
string val = Guid.NewGuid().ToString();
redis.AddItemToList(key, val);
Console.WriteLine(val);
}
Console.WriteLine("done");
}
Upvotes: 2
Views: 1639
Reputation: 20184
An update, some 5 years later:
ServiceStack has added Async support to its Redis libraries, from v 5.10 it seems (read more here):
This release continues to bring improvements across the board with a major async focus on most of ServiceStack’s existing sync APIs gaining pure async implementations allowing your App’s logic to use their preferred sync or async APIs.
Example:
Upvotes: 1
Reputation: 622
There is no pure async as you expect, but you can use workaround below. Its good enough for big load. You can run test from example below and test how it works for you and make decision to use it in your project or not.
public class ad_hook
{
[Fact]
public async Task test_redis_load()
{
var repository = GetRedis();
int expected;
IEnumerable<Task<string>> tasks;
using (var redisRepository = repository)
{
redisRepository.Set("foo", "boo");
expected = 10000;
tasks = Enumerable.Range(1, expected).Select(_ => Task.Run(() => redisRepository.Get<string>("foo")));
var items = await Task.WhenAll(tasks);
items.Should().OnlyContain(s => s == "boo")
.And.HaveCount(expected);
}
}
private static RedisRepository GetRedis()
{
var repository = new RedisRepository();
return repository;
}
}
public class RedisRepository : IDisposable
{
private Lazy<IRedisClient> clientFactory;
private PooledRedisClientManager clientManager;
private T Run<T>(Func<IRedisClient, T> action)
{
using (var client = GetRedisClient())
{
return action(client);
}
}
private IRedisClient GetRedisClient()
{
return clientManager.GetClient();
}
public RedisRepository()
{
clientFactory = new Lazy<IRedisClient>(GetRedisClient);
clientManager = new PooledRedisClientManager();
}
public void Set<T>(string key, T entity)
{
Run(_ => _.Set(key, entity));
}
public T Get<T>(string key)
{
return Run(_ => _.Get<T>(key));
}
public void Dispose()
{
clientManager.Dispose();
if (clientFactory.IsValueCreated)
{
clientFactory.Value.Dispose();
}
}
}
Upvotes: 2