Redis Databases
- Use different Redis databases for different kinds of data. In Redis, databases are identified by an integer index, not by a database name. By default, a client is connected to database 0. With the SELECT command you can switch to a different database:
- redis> select 3
OK
- redis> select 3
- Each Redis database has its own keyspace. By using different databases for your ‘staging’ and ‘production’ data, for example, you don’t have to worry about key clashes between the two
References:
- http://www.rediscookbook.org/multiple_databases.html
- http://stackoverflow.com/questions/13386053/how-do-i-change-between-redis-database
- http://stackoverflow.com/questions/16221563/whats-the-point-of-multiple-redis-databases
- https://www.quora.com/What-are-5-mistakes-to-avoid-when-using-Redis
- http://lzone.de/cheat-sheet/Redis
StackExchange.Redis Basics
- The central object in StackExchange.Redis is the ConnectionMultiplexer class in the StackExchange.Redis namespace; this is the object that hides away the details of multiple servers. 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.
- Accessing a redis database is as simple as:
- IDatabase db = redis.GetDatabase();
The object returned from GetDatabase is a cheap pass-thru object, and does not need to be stored
- RedisKey:
- StackExchange.Redis represents keys by the RedisKey type. The good news, though, is that this has implicit conversions to and from both string and byte[], allowing both text and binary keys to be used without any complication
- RedisValue:
- Values can also need to represent typed primitive data – most commonly (in .NET terms) Int32, Int64, Double or Boolean. Because of this, RedisValue provides a lot more conversion support thanRedisKey
- 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.
- Note additionally that when treated numerically, redis treats a non-existent key as zero; for consistency with this, nil responses are treated as zero:
db.KeyDelete(“abc”);
int i = (int)db.StringGet(“abc”); // this is ZERO
- If you need to detect the nil condition, then you can check for that:
db.KeyDelete(“abc”);
var value = db.StringGet(“abc”);
bool isNil = value.IsNull; // this is true
or perhaps more simply, just use the provided Nullable<T> support:
db.KeyDelete(“abc”);
var value = (int?)db.StringGet(“abc”); // behaves as you would expect
Documentation:
(colored ones just mean I have tried that documentation to some extent)
- Basic Usage – getting started and basic usage
- Configuration – options available when connecting to redis
- Pipelines and Multiplexers – what is a multiplexer?
- Keys, Values and Channels – discusses the data-types used on the API
- Transactions – how atomic transactions work in redis
- Events – the events available for logging / information purposes
- Pub/Sub Message Order – advice on sequential and concurrent processing
- Where are KEYS / SCAN / FLUSH*? – how to use server-based commands
- Profiling – profiling interfaces, as well as how to profile in an async world
- Scripting – running Lua scripts with convenient named parameter replacement
Code:
References: