-
Notifications
You must be signed in to change notification settings - Fork 0
Usage
The URP SDK exposes the class UnifiedConnectionMultiplexer which implements IConnectionMultiplexer class from StackExchange.Redis library. This class can be used to connect to the Redis Cluster. In the most general scenario 4 parameters are needed to connect to the cluster
- Cluster ID – This is the ID of the cluster to which the application has been registered.
- Application ID – This is a unique ID generated for your application
- App Key – A secret is generated for each application which is used for authentication
- [Options] Location – The location of the client application can either be specified when connecting to the cluster. If left blank, then the location is auto detected.
IConnectionMultiplexer _mux = UnifiedConnectionMultiplexer.Connect(_clusterName, _appName, _appSecret, preferredLocation: _location);
All existing operations available on IConnectionMultiplexer is also available on UnifiedConnectionMultiplexer.
Once the connection to the cluster has been established the next step is to create an instance of the Redis Database to perform the required operations. URP SDK exposes the UnifiedRedisDatabase class which is implemented from IDatabase of StackExchange.Redis library, thus providing all operations in StackExchange.Redis. IDatabase _database = _mux.GetDatabase(); All operations available on IDatabase can now be performed. Note that the client application need not add any prefix to the keys, the prefix addition is abstracted and will be taken care by the application. A few examples of using the IDatabase are given below:
await _database.StringSetAsync(keyName, value);
var value = await _database.StringGetAsync(keyName);
To see a full list of operations available in StackExchage.Redis library please check the official documentation of StackExchage.Redis.
Apart from the operations available in IConnectionMultiplexer there are a few extra operations which are also available in UnifiedConnectionMultiplexer class which can be used by the client applications. For using these extra methods, client applications must use the IUnifiedConnectionMultiplexer interface. UnifiedConnectionMultiplexer class implements both IConnectionMultiplexer and IUnifiedConnectionMultiplexer interface. Here are the list of operations:
-
Task<List<RedisKey>> GetKeysAsync(string pattern = "")- Gets all the keys from the Redis cluster (includes all caches in a cluster). Applications can also search for keys with the given pattern. -
List<RedisKey> GetKeys(string pattern = "")- Synchronous version of the above -
Task FlushAsync(string pattern = "", CommandFlags flags = CommandFlags.None)- Can be used to delete all keys from the Primary Redis Cache in the cluster. A pattern can be provided for deleting keys with the given pattern. Here primary Redis cache indicates the cache which is geographically closest to the client application. -
void Flush(string pattern = "", CommandFlags flags = CommandFlags.None)- Synchronous version of the above -
Task FlushSecondaryAsync(string pattern = "", CommandFlags flags = CommandFlags.None)- Used for clearing all keys from the Secondary Redis caches in the cluster. Pattern search can be used. -
void FlushSecondary(string pattern = "", CommandFlags flags = CommandFlags.None)- Synchronous version of the above.