Getting Started
A server in one command, two commands to use it, and the model underneath.
Redis is usually called a cache. It is a server holding data structures in memory, and a cache is one use for that.
Running Redis in one line
No install, no configuration file, no schema:
docker run -d --name rd -p 6379:6379 redis:8-alpine
docker exec -it rd redis-cli PINGredis-cli ships inside the image. Every snippet runs against that container.
redis-cli
With no arguments it opens a prompt showing host, port and database number:
docker exec -it rd redis-cliSET and GET
The two commands you already know:
SET city London
GET citySET always succeeds. Writing over an existing key replaces it, with no warning:
SET city London
SET city Paris
GET cityEverything is a key
A key is the only way in. No tables, no columns, no asking which keys hold what:
SET user:1138:name ada
SET user:1138:email [email protected]
GET user:1138:name
GET user:1138:emailEach key has one type. The wrong command is an error, not a conversion:
SET city London
LPUSH city ParisWRONGTYPE usually means two parts of your code disagree about what a key holds.
TYPE, EXISTS, DEL
Three commands for inspecting what you created:
SET city London
TYPE city
EXISTS city
DEL city
EXISTS cityDEL returns how many it removed, so a missing key is not an error:
SET a 1
SET b 2
DEL a b missingReading a reply
Five reply shapes cover almost everything, and redis-cli labels them:
SET n 10
GET n
INCR n
EXISTS n
GET missing
TYPE missingINFO and DBSIZE
DBSIZE counts the keys in the current database:
DBSIZE
SET a 1
SET b 2
DBSIZEINFO reports on the server itself, in sections:
docker exec -it rd redis-cli INFO server | head -3INFO memory and INFO stats are the ones worth knowing; chapter 10 reads both.
FLUSHALL
It empties every database, immediately:
SET a 1
SET b 2
DBSIZE
FLUSHALL
DBSIZE