Redis is an in-memory database, good to be used as
Installing Redis on ubuntu
sudo apt-get install redis-tools
redis-cli
to enter redis commands nano /etc/redis/redis.conf
and replace bind 127.0.0.1
to bind 0.0.0.0
service redis-server restart
redis-cli -h 192.168.x.x ping
, should get PONG
in return
Python library:
sudo pip install redis
orsudo easy_install install redis
or
select 1
String key/values
users:ololo
, :
doesn't have any specific meaning, but can be used to separate "table name" and "key name"set users:ololo '{"name":"ololo", ...}'
- the content can be any string, not necessarily JSONget users:ololo
String functions:
strlen key
returns the lenth of the value associated with key key
getrange key 0 10
returns the substring from 0 to 10 of the value stored by key
append key value2
appends 'value2' to the current value stored by key
incr key
increment the integer value stored in key
, or create a new key with value 1decr key
decrement the valueincrby
and decrby
are used to increase/decrease the values by some number
setbit
and getbit
hset
and hget
hset hashname field 30
is approximately equal to hashname[field] = 30
hashname
doesn't exist, it will be created. If exists, but field
doesn't, field
will be createdhgetall hashname
returns all fields of the hash hashname
hdel hashname field
removes field
from hashname
lpush listname new_value
adds the new value to the front of listname
ltrim listname 0 49
removes first 50 values from the listlrange listname 0 9
returns first 10 values
Unordered sets
sadd set1 val1 val2 val3
adds values "val1" "val2" "val3" to set setkey
smember set1 val1
sinter set1 set2
intersection of two sets set1
and set2
sinterstore set1 set2 new_set
store instersection of two sets in a new set new_set
Ordered sets
zadd
, zmember
, ...
user:id
, e.g. users:9001
hget user:emails ololo@mlwiki.org
expire key 30
where 30 is the time to live in seconds expireat key unix_timestamp
ttl key
to see how much time the key
has left to live
blpop
and brpop
- return and remove first (last) element of a list - or blocks until something is avialable subscribe q_id
and publish q_it message
A P/S model implemented in Scala: https://gist.github.com/debasishg/7056696