+ All Categories
Home > Software > Redis - StackExchangeClient API

Redis - StackExchangeClient API

Date post: 15-Apr-2017
Category:
Upload: ismaeel-enjreny-cobit-5
View: 135 times
Download: 0 times
Share this document with a friend
16
Redis - StackExchange Eng. Ismail Enjreny Email: [email protected]
Transcript
Page 1: Redis - StackExchangeClient API

Redis - StackExchange

Eng. Ismail Enjreny

Email: [email protected]

Page 2: Redis - StackExchangeClient API

Demo Code

• https://www.dropbox.com/s/bl99hdkteeuadny/06%20-%20Redis%20-%20StackExchange%20Client.zip?dl=0

Page 3: Redis - StackExchangeClient API

StackExchange.Redis

• StackExchange.Redis is a high performance general purpose redis client for .NET languages (C#)

• It is the logical successor to BookSleeve, and is the client developed-by (and used-by) Stack Exchange for busy sites like Stack Overflow

• For the full reasons why this library was created (Read here)

Page 4: Redis - StackExchangeClient API

Features

• High performance multiplexed design, allowing for efficient use of shared connections from multiple calling threads

• Abstraction over redis node configuration: the client can silently negotiate multiple redis servers for robustness and availability

• Convenient access to the full redis feature-set

• Full dual programming model both synchronous and asynchronous usage

• Support for redis "cluster"

Page 5: Redis - StackExchangeClient API

Installation

• StackExchange.Redis can be installed via the nuget UI

• https://www.nuget.org/packages/StackExchange.Redis/

• Or via the nuget package manager console:

• PM> Install-Package StackExchange.Redis

• If you require a strong-named package (because your project is strong-named), then you may wish to use instead:

• PM> Install-Package StackExchange.Redis.StrongName

Page 6: Redis - StackExchangeClient API

Basic Usage

• The central object in StackExchange.Redis is the ConnectionMultiplexer class in the StackExchange.Redis namespace

• Because the ConnectionMultiplexer does a lot, it is designed to be shared and reused between callers

• You should not create a ConnectionMultiplexer per operation. It is fully thread-safe and ready for this usage

Page 7: Redis - StackExchangeClient API

Using a redis database

• IDatabase db = redis.GetDatabase();

• The object returned from GetDatabase is a cheap pass-thru object, and does not need to be stored

• Redis supports multiple databases, this can be optionally specified in the call to GetDatabase

• Once you have the IDatabase, it is simply a case of using the redis commands

Page 8: Redis - StackExchangeClient API

Accessing individual servers

• For maintenance purposes, it is sometimes necessary to issue server-specific commands

• The GetServer method will accept an EndPoint or the name/value pair that uniquely identify the server

• The object returned from GetServer is a cheap pass-thru object that does not need to be stored

Page 9: Redis - StackExchangeClient API

Configuration• Because there are lots of different ways to configure redis, StackExchange.Redis offers a rich

configuration model, which is invoked when calling Connect

• The configuration here can be either:• a ConfigurationOptions instance

• a string representing the configuration

• var conn = ConnectionMultiplexer.Connect("localhost");

• var conn = ConnectionMultiplexer.Connect("redis0:6380,redis1:6380,allowAdmin=true");

• The mapping between the string and ConfigurationOptions representation is shown below

• ConfigurationOptions options = ConfigurationOptions.Parse(configString);

• Or string configString = options.ToString();

Page 10: Redis - StackExchangeClient API

Renaming Commands

• A slightly unusual feature of redis is that you can disable and/or rename individual commands

Page 11: Redis - StackExchangeClient API

Keys, Values and Channels

• A key is the unique name of a piece of data (which could be a String, a List, Hash, or any of the other redis data types) within a database

• When using pub/sub, we are dealing with channels; channels do not affect routing (so they are not keys), but are quite distinct from regular values, so are considered separately.

• Keys• StackExchange.Redis represents keys by the RedisKey type

• It has implicit conversions to and from both string and byte[], allowing both text and binary keys to be used without any complication

Page 12: Redis - StackExchangeClient API

Keys, Values and Channels

• Values• StackExchange.Redis represents values by the RedisValue type

• As with RedisKey, there are implicit conversions in place which mean that most of the time you never see this type

• Note that while the conversions from primitives to RedisValue are implicit, many of the conversions from RedisValue to primitives are explicit

• This is because it is very possible that these conversions will fail if the data does not have an appropriate value

Page 13: Redis - StackExchangeClient API

Transactions in Redis

• A transaction in redis consists of a block of commands placed between MULTI and EXEC (or DISCARD for rollback)

• Once a MULTI has been encountered, the commands on that connection are not executed - they are queued (and the caller gets the reply QUEUED to each)

• When an EXEC is encountered, they are all applied in a single unit

• If a DISCARD is seen instead of a EXEC, everything is thrown away

Page 14: Redis - StackExchangeClient API

Transactions in Redis (Cont. …)

• Once the transaction is open you can't fetch data - your operations are queued

• WATCH {key} tells Redis that we are interested in the specified key for the purposes of the transaction

• Redis will automatically keep track of this key, and any changes will essentially doom our transaction to rollback

• Note that the object returned from CreateTransaction only has access to the async methods - because the result of each operation will not be known until after Execute (or ExecuteAsync) has completed

• If the operations are not applied, all the Tasks will be marked as cancelled - otherwise, after the command has executed you can fetch the results of each as normal.

Page 15: Redis - StackExchangeClient API

Inbuilt operations via When

• It should also be noted that many common scenarios (in particular: key/hash existence, like in the above) have been anticipated by Redis, and single-operation atomic commands exist

Page 16: Redis - StackExchangeClient API

Thanks


Recommended