Philippe Lafoucrière / Tech-Angels / @plafoucriere
From Redis homepage:
Redis is an open source, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets
Developed by Salvatore Sanfilippo / @antirez
Redis means REmote DIctionary Server.
"Publish–subscribe pattern"
Using the MULTI and EXEC commands, it is possible to send several commands at once to Redis.
Anyway, Redis transactions are very different from SQL transactions: In case of failure, there is NO rollback.
Commands are evaluated during queue process, and EXEC will refuse to run.
"EVAL and EVALSHA are used to evaluate scripts using the Lua interpreter built into Redis starting from version 1.6.0."
redis 127.0.0.1:6379[1]> set mykey "compiegne RB"
OK
redis 127.0.0.1:6379[1]> get mykey
"compiegne RB"
>> require 'rubygems'
=> false
>> require 'redis'
=> true
>> r = Redis.new
=> #
>> r.ping
=> "PONG"
>> r.set('foo','bar')
=> "OK"
>> r.get('foo')
=> "bar"
> hset configuration secret_key donttellanyone
(integer) 0
> hget configuration secret_key
"donttellanyone"
> rpush my_queue "do_the_job:[123,\"another_params\"]"
(integer) 1
> rpop my_queue
"do_the_job:[123,\"another_params\"]"
> rpop my_queue
(nil)
> sadd user:123 "Dark Knight"
(integer) 1
> sadd user:123 "Abyss"
(integer) 1
> sadd user:123 "Abyss"
(integer) 0
> smembers user:123
1) "Abyss"
2) "Dark Knight"
> sadd user:456 "Dark Knight"
(integer) 1
> sadd user:456 "Paul"
(integer) 1
> sinter user:123 user:456
1) "Dark Knight"
> incr package:123:downloads
(integer) 1
> incr package:123:downloads
(integer) 2
> incr package:123:downloads
(integer) 3
> incrby package:123:downloads 3
(integer) 6
Time complexity: O(1)
Connection A:
> blpop job_queue 10
1) "job_queue"
2) "do_the_job:[123,\"another_params\"]"
(2.87s)
Connection B:
> rpush job_queue "do_the_job:[123,\"another_params\"]"
(integer) 1
> rpush job_queue "do_the_job:[456,\"another_params\"]"
(integer) 1
> rpush job_queue "do_the_job:[789,\"another_params\"]"
(integer) 2
Time complexity: O(1)
Set list of friends:
> sadd user:123:friends 456 789
(integer) 2
> smembers user:123:friends
1) "456"
2) "789"
After sign-in, add user id to a set (1 set every 10 minutes):
> sadd online_users:2012-12-20-20:00 123 789
(integer) 2
> sinter user:123:friends online_users:2012-12-20-20:00
1) "789"
Time complexity: O(1)