зеркало из
1
0
Форкнуть 0
1 URP SDK in Server Mode
Pratik Bhattacharya редактировал(а) эту страницу 2021-04-18 22:14:50 +05:30

Using URP in Server Mode would imply that you already have a Web Server setup to store the configurations for the Clusters and Applications. The URP SDK will connect to the APIs exposed by the Server to get the connection and other required configurations. The URP SDK will only need to supply Cluster ID, App ID and App Secret to connect to the common Redis Cache Cluster.

Installation

You can install the SDK from the global NuGet feed.

Install-Package UnifiedRedisPlatform

Connecting to Cluster

The URP SDK exposes the UnifiedConnectionMultiplexer class which implements IConnectionMultiplexer interface from StackExchange.Redis library. You can use this class to connect to the Redis Cluster. In the most general scenarios four parameters are needed to connect to the Cluster:

  1. Cluster ID – This is the Cluster ID under which the Application has been registered.
  2. Application ID – This is a unique ID of your Application
  3. App Key – Application secret for authenticating the calls
  4. [Optional] Location – The location parameter is required to get the Redis Cache located closest to the Client Application. 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 methods on IConnectionMultiplexer is also available on UnifiedConnectionMultiplexer.

Creating Redis Database

With the connection established, the next step is to create an instance of the Redis Database to interact with the cache. URP SDK exposes the UnifiedRedisDatabase class, which implements IDatabase of the StackExchange.Redis library.

IDatabase _database = _mux.GetDatabase();

You can now invoke all methods in IDatabase. Note that the client application need not add any prefix to the keys; the prefix addition to the key is abstracted and will be taken care of by the URP SDK. Here are examples to use IDatabase:

await _database.StringSetAsync(keyName, value);
string value = await _database.StringGetAsync(keyName);

To see a full list of operations available in the StackExchage.Redis library, please check the official documentation of StackExchage.Redis.

Other Connection Multiplexer level operations

Apart from the operations available in IConnectionMultiplexer, there are additional methods available in the UnifiedConnectionMultiplexer class which the client applications can use. For using these additional methods, client applications must use the IUnifiedConnectionMultiplexer interface while creating the ConnectionMultiplexer. Internally IUnifiedConnectionMultiplexer extends from IConnectionMultiplexer and UnifiedConnectionMultiplexer class implements IUnifiedConnectionMultiplexer interface. Here is the list of additional methids:

  1. 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.
  2. List<RedisKey> GetKeys(string pattern = "") - Synchronous version of the above
  3. Task FlushAsync(string pattern = "", CommandFlags flags = CommandFlags.None) - Can be used to delete all keys from the Primary Redis Cache in the cluster. If a pattern is provided, then URP SDK will delete the keys following the given pattern. Here Primary Redis Cache indicates the Cache which is geographically closest to the client application.
  4. void Flush(string pattern = "", CommandFlags flags = CommandFlags.None) - Synchronous version of the above
  5. 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.
  6. void FlushSecondary(string pattern = "", CommandFlags flags = CommandFlags.None) - Synchronous version of the above. For all the above operations, client applications need not consider the key prefix. The SDK will add the Key prefix for all these operations.

The pattern parameters accept wildcard character '*'. So a pattern *Mapping* would search for all keys which contain the string Mapping.

Example usage

IUnifiedConnectionMultiplexer connectionMux = UnifiedConnectionMultiplexer.Connect("Cluster ID", "App ID", "App Secret", preferredLocation: "Location")
IEnumerable<RedisKey> = await _connectionMultiplexer.GetKeysAsync();