Qoollo.Redis.Net is a convinient wrapper over StackExchange.Redis which provide you simple tool to work with Redis in your .Net Framework and .Net Core projects.
Qoollo.Redis.Net can be installed via the nuget UI (as Qoollo.Redis.Net), or via the nuget package manager console:
PM> Install-Package Qoollo.Redis.Net -Version 1.0.5
After that you need to specify Redis configuration section in appsettings.json
...
"Redis": {
"RedisIp": "<some_ip>",
"RedisPort": 6379,
"RedisSyncOperationsTimeoutMs": 5000,
"RedisBlockingOperationsTimeoutMs": 500,
"ClientName": "<some_client_name>"
},
...
Finally you can simply registry RedisService
...
services.AddRedisService(Configuration.GetSection("Redis"));
...
This wrapper provides IRedisService
interface via which one you can inject RedisService
dependecy in your class, such as:
public class MyClass
{
private readonly IRedisService _redisService;
public MyClass(IRedisService redisService)
{
_redisService = redisService;
}
}
IRedisService
provides the following methods to use:
Property signature: bool IsConnected { get; }
Description: True if connection to Redis established, false if not.
Method signature: void Connect();
Description: Connect to Redis.
Method signature: void Disconnect();
Description: Disconnect from Redis.
Method signature: void SubscribeToChannel(string channel, Action<string, byte[]> handler);
Description: Subscribe handler to channel.
Exception: RedisNotConnectedException
- Raised when there is no connection to Redis.
Parameters:
channel
- Channel's name.handler
- Handler for event of receiving message from channel. First arg is channel's name (string), second is received message from redis (byte[]).
Method signature: void PublishToChannel(string channel, byte[] data);
Description: Publish data to channel.
Exception: RedisNotConnectedException
- Raised when there is no connection to Redis.
Parameters:
channel
- Channel's name.data
- Publishing data
Method signature: Task<bool> SetKeyAsync(string key, byte[] data);
Description: Adds key with given data to Redis.
Exception: RedisNotConnectedException
- Raised when there is no connection to Redis..
Returns: True if key has been already existed and was overwritten, false if not.
Method signature: Task<bool> SetKeyWithExpirationTimeAsync(string key, byte[] data, TimeSpan expirationTime);
Description: Adds key with given data to Redis, that will be expired (automatically removed) after given expiratin time.
Exception: RedisNotConnectedException
- Raised when there is no connection to Redis..
Returns: True if key has been already existed and was overwritten, false if not.
Method signature: Task<bool> DeleteKeyAsync(string key);
Description: Removes the specified key. A key is ignored if it does not exist.
Exception: RedisNotConnectedException
- Raised when there is no connection to Redis..
Returns: True if the key was removed.
Method signature: Task RequestForRemovingKeyAsync(string key);
Description: Sends request for removing the specified key (fire and forget). A key is ignored if it does not exist.
Exception: RedisNotConnectedException
- Raised when there is no connection to Redis..
Method signature: Task<byte[]> GetValueAsync(string key);
Description: Removes the specified key. A key is ignored if it does not exist.
Exception: RedisNotConnectedException
- Raised when there is no connection to Redis..
Returns: Value or null if key doesn't exist.
Method signature: Task<List<byte[]>> GetValuesAsync(IEnumerable<string> keys);
Description: Get values from Redis by theirs' keys.
Exception: RedisNotConnectedException
- Raised when there is no connection to Redis..
Returns: List of results, where each result could be value or null if key doesn't exist.
Method signature: Task<List<string>> GetKeysByPatternAsync(string pattern);
Description: Get keys from Redis which match given pattern.
Exception: RedisNotConnectedException
- Raised when there is no connection to Redis.
Parameters: pattern
- Pattern for keys.
Method signature: Task<long> ListLeftPushAsync(string key, byte[] data);
Description: Insert the specified value at the head of the list stored at key. If key does not exist, it is created as empty list before performing the push operations.
Exception: RedisNotConnectedException
- Raised when there is no connection to Redis.
Returns: The length of the list after the push operations.
Method signature: Task<long> ListRightPushAsync(string key, byte[] data);
Description: Insert the specified value at the end of the list stored at key. If key does not exist, it is created as empty list before performing the push operations.
Exception: RedisNotConnectedException
- Raised when there is no connection to Redis.
Returns: The length of the list after the push operations.
Method signature: Task<long> ListRightPushAsync(string key);
Description: Pop value from the end of the list stored at key.
Exception: RedisNotConnectedException
- Raised when there is no connection to Redis.
Returns: The last element of the list stored at key.
Method signature: Task<long> ListLeftPushAsync(string key);
Description: Pop value from the head of the list stored at key.
Exception: RedisNotConnectedException
- Raised when there is no connection to Redis.
Returns: The first element of the list stored at key.
Method signature: Task<long> ListRightPushAsync(string key, CancellationToken stoppingToken);
Description: Pop value from the end of the list stored at key. If list is empty, operation blocks until someone push data to the list.
Exception: RedisNotConnectedException
- Raised when there is no connection to Redis.
Returns: The last element of the list stored at key.
Method signature: Task<long> ListLeftPushAsync(string key, CancellationToken stoppingToken);
Description: Pop value from the head of the list stored at key. If list is empty, operation blocks until someone push data to the list.
Exception: RedisNotConnectedException
- Raised when there is no connection to Redis.
Returns: The first element of the list stored at key.
Method signature: Task<long> ListRemoveAsync(string key, byte[] data);
Description: Removes specified value from the list stored under key.
Exception: RedisNotConnectedException
- Raised when there is no connection to Redis.
Returns: The length of the list after the remove operations.
Method signature: Task<List<byte[]>> GetListAsync(string key);
Description: Get all elements from list in Redis by given key.
Exception: RedisNotConnectedException
- Raised when there is no connection to Redis.
Parameters: key
- Specified key.
Returns: List with all elements from list in Redis