tips:db:redis

redis

type string: store a value [integer, string, …] belong a key [string]

SET connections 10
INCR connections => 11
INCR connections => 12
DEL connections
INCR connections => 1

expiring key

SET resource:lock "Redis Demo"
EXPIRE resource:lock 120

A list have an order

RPUSH puts the new value at the end of the list. LPUSH puts the new value at the start of the list. LLEN, LPOP, RPOP

RPUSH friends "Alice"
RPUSH friends "Bob"
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"

A set does not have an order but each element may only appear once.

SADD superpowers "flight"
SADD superpowers "x-ray vision"
SADD superpowers "reflexes"
SREM superpowers "reflexes"

SISMEMBER superpowers "flight" => 1
SISMEMBER superpowers "reflexes" => 1

A sorted set is similar to a regular set, but now each value has an associated score. This score is used to sort the elements in the set.

ZADD hackers 1940 "Alan Kay"
ZADD hackers 1969 "Linus Torvalds"
ZADD hackers 1906 "Grace Hopper"
ZADD hackers 1953 "Richard Stallman"

ZRANGE hackers 2 4
1) "Richard Stallman"
2) "Linus Torvalds"

type hash for tables and rows

namespace:table:id

INCR gianoauth:realm:next_user_id => 1
HSET gianoauth:realm:user:1 id 1 username "scipio" name "Stefano Scipioni" password "hash" visits 0

add an INDEX with sets: collections of strings that are unordered and cannot contain duplicates

SADD gianoauth:realm:all-users gianoauth:realms:users:1

visits

HINCRBY gianoauth:realm:user:1 visits 1 => 2

use multi exec to surround HSET and SADD

MULTI
HSET ...
HSET ...
SADD ...
EXEC

sorted set: like a set a sorted set only allows members without repeats, but also allows you to specify a score

ZADD gianoauth:realms:priceIndex 10.99 scipio
sudo apt install -y redis-server redis-tools
  • tips/db/redis.txt
  • Last modified: 2017/11/07 13:44
  • by scipio