Redis
Redis is an open source, BSD licensed, advanced key-value cache and store. When to redis when to mongodb
The Redis Interactive Tutorial is a great way to get started. Here some output I got while trying it out ...
Set the value foo vor the key k
>SET k "foo"
OK
>GET k
"foo"
OK
>GET k
"foo"
You may use seperators in the keys, e.g.
>SET groupABC:user123:time001 "foo"
OK
OK
Atomic value changes
>SET myCounter 42
OK
>INCR myCounter
(INTEGER) 43
>INCR myCounter
(INTEGER) 44
>DEL myCounter
(INTEGER) 1
>INCR myCounter
(INTEGER) 1
OK
>INCR myCounter
(INTEGER) 43
>INCR myCounter
(INTEGER) 44
>DEL myCounter
(INTEGER) 1
>INCR myCounter
(INTEGER) 1
Autoexpire values
>SET resource:LOCK "Redis Demo"
OK
>EXPIRE resource:LOCK 120
(INTEGER) 1
>TTL resource:LOCK
(INTEGER) 115
>TTL resource:LOCK
(INTEGER) 106
OK
>EXPIRE resource:LOCK 120
(INTEGER) 1
>TTL resource:LOCK
(INTEGER) 115
>TTL resource:LOCK
(INTEGER) 106
Setting values resets its TTL
> SET resource:LOCK "Redis Demo 1"
OK
> EXPIRE resource:LOCK 120
(INTEGER) 1
> TTL resource:LOCK
(INTEGER) 119
> SET resource:LOCK "Redis Demo 2"
OK
> TTL resource:LOCK
(INTEGER) -1
OK
> EXPIRE resource:LOCK 120
(INTEGER) 1
> TTL resource:LOCK
(INTEGER) 119
> SET resource:LOCK "Redis Demo 2"
OK
> TTL resource:LOCK
(INTEGER) -1
List
>RPUSH friends "Alice"
(INTEGER) 1
>RPUSH friends "Bob"
(INTEGER) 2
>LPUSH friends "Sam"
(INTEGER) 3
>LRANGE friends 0 -1
1) "Sam"
2) "Alice"
3) "Bob"
>LRANGE friends 0 1
1) "Sam"
2) "Alice"
>LRANGE friends 1 2
1) "Alice"
2) "Bob"
>LLEN friends
(INTEGER) 3
>LPOP friends
"Sam"
>RPOP friends
"Bob"
>LLEN friends
(INTEGER) 1
>LRANGE friends 0 -1
1) "Alice"
(INTEGER) 1
>RPUSH friends "Bob"
(INTEGER) 2
>LPUSH friends "Sam"
(INTEGER) 3
>LRANGE friends 0 -1
1) "Sam"
2) "Alice"
3) "Bob"
>LRANGE friends 0 1
1) "Sam"
2) "Alice"
>LRANGE friends 1 2
1) "Alice"
2) "Bob"
>LLEN friends
(INTEGER) 3
>LPOP friends
"Sam"
>RPOP friends
"Bob"
>LLEN friends
(INTEGER) 1
>LRANGE friends 0 -1
1) "Alice"
Set, add, remove and test for element
> SADD superpowers "flight"
(INTEGER) 1
> SREM superpowers "reflexes"
0
> SISMEMBER superpowers "flight"
(INTEGER) 1
(INTEGER) 1
> SREM superpowers "reflexes"
0
> SISMEMBER superpowers "flight"
(INTEGER) 1
Sorted set, with value to sort with
> ZADD hackers 1940 "Alan Kay"
Hashes, more than one value for a key
> HSET USER:1000 name "John Smith"
(INTEGER) 1
> HSET USER:1000 email "john.smith@example.com"
(INTEGER) 1
> HSET USER:1000 password "s3cret"
(INTEGER) 1
> HGETALL USER:1000
1) "name"
2) "John Smith"
3) "email"
4) "john.smith@example.com"
5) "password"
6) "s3cret"
(INTEGER) 1
> HSET USER:1000 email "john.smith@example.com"
(INTEGER) 1
> HSET USER:1000 password "s3cret"
(INTEGER) 1
> HGETALL USER:1000
1) "name"
2) "John Smith"
3) "email"
4) "john.smith@example.com"
5) "password"
6) "s3cret"
Increment value in Hash
> HSET USER:1000 visits 10
(INTEGER) 1
> HINCRBY USER:1000 visits 1
(INTEGER) 11
(INTEGER) 1
> HINCRBY USER:1000 visits 1
(INTEGER) 11