Last year I wrote an article about the right way to set up a Redis connection using StackExchange’s Redis client library. A lot of people found this useful, but at the same time the article went into a lot of detail in order to explain the dangers of doing this wrong. Also, there is a connection string format that’s a lot more concise.
So here’s how you set up a Redis connection using StackExchange.Redis, really quickly. If you need to just try this out quickly, you can grab a copy of Redis for Windows (just remember that this is not supported for production environments).
First, install the NuGet package for the Redis client library:
Then, figure out what connection you need, and build a lazy factory for it (ideally the connection string should come from a configuration file):
private static Lazy<ConnectionMultiplexer> conn = new Lazy<ConnectionMultiplexer>( () => ConnectionMultiplexer.Connect( "localhost:6379,abortConnect=false,syncTimeout=3000"));
My original article goes into detail on why this lazy construct is necessary, but it is mainly because it guarantees thread safety, so the
ConnectionMultiplexer will be created only once when it is needed (which is how it’s intended to be used).
You build up a connection string using a comma-separated sequence of configuration parameters (as an alternative to
ConfigurationOptions in code, which the original article used). This is more concise but also makes configuration a lot easier.
At the very least, you should have one or more endpoints that you’ll connect to (6379 is the default port in case you leave it out),
abortConnect=false to automatically reconnect in case a disconnection occurs (see my original article for details on this), and a reasonable
syncTimeout in case some of your Redis operations take long.
The default for
syncTimeout is 1 second (i.e. 1000, because the value in milliseconds), and operations against Redis should typically be a lot less than that. But we don’t work in an ideal world, and since Redis is single-threaded, expensive application commands against Redis or even internal Redis operations can cause commands to at times exceed this threshold and result in a timeout. In such cases, you don’t want an operation to fail because of a one-off spike, so just give it a little extra (3 seconds should be reasonable). However, if you get lots of timeouts, you should review your code and look for bottlenecks or blocking operations.
Once you have the means to create a connection (as above), just get the lazy value, and from it get a handle on one of the 16 Redis databases (by default it’s database 0 if not specified):
var db = conn.Value.GetDatabase();
I’ve seen a lot of code in the past that just calls
GetDatabase() all the time, for each operation. That’s fine, because the Basic Usage documentation states that:
“The object returned from
GetDatabaseis a cheap pass-thru object, and does not need to be stored.”
Despite this, I see no point in having an unnecessary extra level of indirection in my code, so I like to store this and work directly with it. Your mileage may vary.
Once you’ve got hold of your Redis database, you can perform your regular Redis operations on it.
db.StringSet("x", 1); var x = db.StringGet("x");