Install via Nuget > Install-Package TomLonghurst.RedisClient
Create a RedisClientConfig
object:
var config = new RedisClientConfig(Host, Port, Password) {
Ssl = true,
Timeout = 5000
};
Create a new RedisClientManager
object:
int poolSize = 5;
var redisManager = new RedisClientManager(config, poolSize);
Call RedisClientManager.GetRedisClientAsync()
var client = await redisManager.GetRedisClientAsync();
Each Redis Client can only perform one operation at a time. Because it's usually very fast, one is enough for most applications.
However if your application takes heavy traffic, and you are seeing RedisTimeoutException
s then consider upping the pool size.
var ping = await client.Ping();
await _client.StringSetAsync("key", "123", AwaitOptions.FireAndForget);
await _client.StringSetAsync("key", "123", 120, AwaitOptions.FireAndForget);
var keyValues = new List<KeyValuePair<string, string>>()
{
new KeyValuePair<string, string>("key1", "1"),
new KeyValuePair<string, string>("key2", "2"),
new KeyValuePair<string, string>("key3", "3")
};
await _client.StringSetAsync(keyValues, AwaitOptions.AwaitCompletion);
var value = await _client.StringGetAsync("key");
var values = await _client.StringGetAsync(new [] { "key1", "key2" });
await _client.DeleteKeyAsync("key", AwaitOptions.AwaitCompletion);
await _client.DeleteKeyAsync(new [] { "key1", "key2" }, AwaitOptions.AwaitCompletion);
var exists = await _client.KeyExistsAsync("key");
Any method taking an AwaitOptions parameter has two options:
Wait for the operation to complete on the Redis server before resuming program execution
Resume with program execution instantly and forget about checking the result
If you enjoy, please buy me a coffee :)