Notes From The Little Redis Book

I am working on a small Ruby app that uses Redis. It won’t really do much. I am just doing it to work with Redis a bit. I am using the Redis gem.

For my reference, here are some commands from The Little Redis Book. (I think this is allowed under the license.)

https://github.com/karlseguin/the-little-redis-book/blob/master/en/redis.md
set users:leto “{name: leto, planet: dune, likes: [spice]}”
get users:leto
set users:leto “{name: leto, planet: dune, likes: [spice]}”
> strlen users:leto
(integer) 42

> getrange users:leto 27 40
“likes: [spice]”

> append users:leto ” OVER 9000!!”
(integer) 54
> incr stats:page:about
(integer) 1
> incr stats:page:about
(integer) 2

> incrby ratings:video:12333 5
(integer) 5
> incrby ratings:video:12333 3
(integer) 8
hset users:goku powerlevel 9000
hget users:goku powerlevel

hmset users:goku race saiyan age 737
hmget users:goku race powerlevel
hgetall users:goku
hkeys users:goku
hdel users:goku age

lpush newusers goku
ltrim newusers 0 50

keys = redis.lrange(‘newusers’, 0, 10)
redis.mget(*keys.map {|u| “users:#{u}”})

sadd friends:leto ghanima paul chani jessica
sadd friends:duncan paul jessica alia

sismember friends:leto jessica
sismember friends:leto vladimir

sinter friends:leto friends:duncan

sinterstore friends:leto_duncan friends:leto friends:duncan

zadd friends:duncan 70 ghanima 95 paul 95 chani 75 jessica 1 vladimir

zcount friends:duncan 90 100

zrevrank friends:duncan chani

Has a good summary of Big O notation

Bad command:
set users:leto@dune.gov “{id: 9001, email: ‘leto@dune.gov’, …}”
set users:9001 “{id: 9001, email: ‘leto@dune.gov’, …}”

better:
set users:9001 “{id: 9001, email: leto@dune.gov, …}”
hset users:lookup:email leto@dune.gov 9001

get users:9001

id = redis.hget(‘users:lookup:email’, ‘leto@dune.gov’)
user = redis.get(“users:#{id}”)

sadd friends:leto ghanima paul chani jessica

sadd friends_of:chani leto paul

keys = redis.lrange(‘newusers’, 0, 10)
redis.mget(*keys.map {|u| “users:#{u}”})

sadd friends:vladimir piter
sadd friends:paul jessica leto “leto II” chani

Ruby:
redis.pipelined do
9001.times do
redis.incr(‘powerlevel’)
end
end

A transaction:
multi
hincrby groups:1percent balance -9000000000
hincrby groups:99percent balance 9000000000
exec

In Ruby:
redis.multi()
current = redis.get(‘powerlevel’)
redis.set(‘powerlevel’, current + 1)
redis.exec()

Or better yet:
redis.watch(‘powerlevel’)
current = redis.get(‘powerlevel’)
redis.multi()
redis.set(‘powerlevel’, current + 1)
redis.exec()

Bad idea in production:
keys bug:1233:*

Better:
hset bugs:1233 1 “{id:1, account: 1233, subject: ‘…’}”
hset bugs:1233 2 “{id:2, account: 1233, subject: ‘…’}”

expire pages:about 30
expireat pages:about 1356933600

ttl pages:about
persist pages:about

setex pages:about 30 ‘<h1>about us</h1>….’

It has pub/sub. Can it be used instead of/in place of something like Rabbit or ZeroMQ?
subscribe warnings

publish warnings “it’s over 9000!”

config set slowlog-log-slower-than 0

slowlog get
slowlog get 10

rpush users:leto:guesses 5 9 10 2 4 10 19 2
sort users:leto:guesses

sadd friends:ghanima leto paul chani jessica alia duncan
sort friends:ghanima limit 0 3 desc alpha

sadd watch:leto 12339 1382 338 9338

set severity:12339 3
set severity:1382 2
set severity:338 5
set severity:9338 4

sort watch:leto by severity:* desc

A group:
hset bug:12339 severity 3
hset bug:12339 priority 1
hset bug:12339 details “{id: 12339, ….}”

hset bug:1382 severity 2
hset bug:1382 priority 2
hset bug:1382 details “{id: 1382, ….}”

hset bug:338 severity 5
hset bug:338 priority 3
hset bug:338 details “{id: 338, ….}”

hset bug:9338 severity 4
hset bug:9338 priority 2
hset bug:9338 details “{id: 9338, ….}”
end of group

sort watch:leto by bug:*->priority get bug:*->details

sort watch:leto by bug:*->priority get bug:*->details store watch_by_priority:leto

config get *log*

rename-command CONFIG 5ec4db169f9d4dddacbfb0c26ea7e5ef
rename-command FLUSHALL 1041285018a942a4922cbf76623b741e

“type $KEY_NAME” is a good command as well

Image from the Rheinau Psalter, a 13th century manuscript housed at Central Library of Zurich. Image from e-Codices. This image is assumed to be allowed under Fair Use.

There are 600 words in this article.