All Projects → thomhurst → A-sync-RedisClient

thomhurst / A-sync-RedisClient

Licence: Apache-2.0 license
An asynchronous Redis Client for .NET!

Programming Languages

C#
18002 projects
powershell
5483 projects

A[sync]RedisClient

nuget Codacy Badge

Please note this is still in development

Install

Install via Nuget > Install-Package TomLonghurst.AsyncRedisClient

Usage

Connect

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();

Pool Size

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 RedisTimeoutExceptions then consider upping the pool size.

Commands

Ping

var ping = await client.Ping();

Set

await _client.StringSetAsync("key", "123", AwaitOptions.FireAndForget);

Set with TimeToLive

await _client.StringSetAsync("key", "123", 120, AwaitOptions.FireAndForget);

Multi Set

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);

Get

var value = await _client.StringGetAsync("key");

Multi Get

var values = await _client.StringGetAsync(new [] { "key1", "key2" });

Delete

await _client.DeleteKeyAsync("key", AwaitOptions.AwaitCompletion);

Multi Delete

await _client.DeleteKeyAsync(new [] { "key1", "key2" }, AwaitOptions.AwaitCompletion);

Key Exists

var exists = await _client.KeyExistsAsync("key");

AwaitOptions

Any method taking an AwaitOptions parameter has two options:

AwaitCompletion

Wait for the operation to complete on the Redis server before resuming program execution

FireAndForget

Resume with program execution instantly and forget about checking the result

If you enjoy, please buy me a coffee :)

Buy Me A Coffee

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].