Redis 101 - My Humble Notes. 
Basic Commands:
[acool@localhost ~]$ redis-server --version
Redis server v=2.8.18 sha=00000000:0 malloc=jemalloc-3.6.0 bits=64 build=94164e7f0a3c47c9
[acool@localhost ~]$
[acool@localhost ~]$ sudo systemctl start redis
[acool@localhost ~]$ redis-cli
127.0.0.1:6379>
127.0.0.1:6379> INFO
127.0.0.1:6379>
127.0.0.1:6379> SET fullName "Angel Cool"
OK
127.0.0.1:6379> GET fullName
"Angel Cool"
127.0.0.1:6379> DEL fullName
(integer) 1
127.0.0.1:6379> GET fullName
(nil)
127.0.0.1:6379> INCR attempts
(integer) 1
127.0.0.1:6379> INCR attempts
(integer) 2
127.0.0.1:6379> GET attempts
"2"
127.0.0.1:6379> SETNX attempts 25 //set if not exist
(integer) 0
127.0.0.1:6379> GET attempts
"2"
127.0.0.1:6379> SET token abc123
OK
127.0.0.1:6379> EXPIRE token 60
(integer) 1
127.0.0.1:6379> TTL token
(integer) 51
127.0.0.1:6379> TTL token
(integer) 44
127.0.0.1:6379> SETEX token 120 321abc //set & expire but atomic ;)
OK
127.0.0.1:6379> TTL token
(integer) 116
127.0.0.1:6379> TTL token
(integer) 114
127.0.0.1:6379> keys * //get all keys in redis
1) "friends"
2) "attempts"
3) "TOKEN"
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> type friends // get key type, possible types: string, list, set, zset, hash.
list
127.0.0.1:6379> type attempts
string


Lists:
127.0.0.1:6379> RPUSH friends Daniel  //push a new value at the end of a list
(integer) 1
127.0.0.1:6379> RPUSH friends William
(integer) 2
127.0.0.1:6379> RPUSH friends Alex
(integer) 3
127.0.0.1:6379> RPUSH friends Rene
(integer) 4
127.0.0.1:6379> LRANGE friends 0 -1 //get all entries in list
1) "Daniel"
2) "William"
3) "Alex"
4) "Rene"
127.0.0.1:6379> LPUSH friends Elizabeth //push a new value at the start of a list
(integer) 5
127.0.0.1:6379> LLEN friends //returns the current lenght of a list
(integer) 5
127.0.0.1:6379> LRANGE friends 2 3 //returns a subset of a list
1) "William"
2) "Alex"
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> LPOP friends //removes the first element from the list and returns it
"Elizabeth"
127.0.0.1:6379> RPOP friends //removes the last element from the list and returns it
"Rene"
127.0.0.1:6379>


Sets:
127.0.0.1:6379> SADD countries Canada
(integer) 1
127.0.0.1:6379> SADD countries Chile
(integer) 1
127.0.0.1:6379> SADD countries Brazil
(integer) 1
127.0.0.1:6379> SADD countries Mexico
(integer) 1
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> SMEMBERS countries //output all members of a set
1) "Canada"
2) "Mexico"
3) "Brazil"
4) "Chile"
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> SISMEMBER countries Mexico //test if a value is in a set
(integer) 1
127.0.0.1:6379> SREM countries Mexico //removes a value from a set
(integer) 1
127.0.0.1:6379> SISMEMBER countries Mexico
(integer) 0
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> SMEMBERS cities
1) "London"
2) "Los Angeles"
3) "Merida"
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> SUNION cities countries //combines sets and returns all members
1) "Merida"
2) "London"
3) "Canada"
4) "Brazil"
5) "Los Angeles"
6) "Chile"


Sorted Sets:
127.0.0.1:6379> ZADD hackers 1940 "Alan Kay"
(integer) 1
127.0.0.1:6379> ZADD hackers 1906 "Grace Hopper"
(integer) 1
127.0.0.1:6379> ZADD hackers 1953 "Richard Stallman"
(integer) 1
127.0.0.1:6379> ZADD hackers 1965 "Yukihiro Matsumoyo"
(integer) 1
127.0.0.1:6379> ZADD hackers 1983 "Angel Cool"
(integer) 1
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> ZRANGE hackers 2 3 WITHSCORES
1) "Richard Stallman"
2) "1953"
3) "Yukihiro Matsumoyo"
4) "1965"
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> ZADD hackers 1980 "Karla Cool"
(integer) 1
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> ZRANGE hackers 0 -1 WITHSCORES //all scores, see how it's sorted by score (date of birth)
1) "Grace Hopper"
2) "1906"
3) "Alan Kay"
4) "1940"
5) "Richard Stallman"
6) "1953"
7) "Yukihiro Matsumoyo"
8) "1965"
9) "Karla Cool"
10) "1980"
11) "Angel Cool"
12) "1983"
127.0.0.1:6379>
127.0.0.1:6379> ZREVRANGE hackers 0 -1 withscores // reverse score
1) "Angel Cool"
2) "1983"
3) "Yukihiro Matsumoyo"
4) "1965"
5) "Richard Stallman"
6) "1953"
7) "Alan Kay"
8) "1940"
9) "Grace Hopper"
10) "1906"
127.0.0.1:6379>
127.0.0.1:6379> ZCOUNT hackers -inf +inf // returns the number of items in zset
(integer) 5
127.0.0.1:6379>
127.0.0.1:6379> ZREMRANGEBYSCORE hackers 1940 1960
(integer) 2


Hashes
127.0.0.1:6379> HSET user:1000 firstName Angel
(integer) 1
127.0.0.1:6379> HSET user:1000 lastName Cool
(integer) 1
127.0.0.1:6379> HSET user:1000 website angelcool.net
(integer) 1
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> HGET user:1000 firstName
"Angel"
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> HMSET user:2000 firstName Richard lastName Stallman website stallman.org
OK
127.0.0.1:6379>
127.0.0.1:6379> HGET user:2000 website
"stallman.org"
127.0.0.1:6379>
127.0.0.1:6379> HGETALL user:2000
1) "firstName"
2) "Richard"
3) "lastName"
4) "Stallman"
5) "website"
6) "stallman.org"
127.0.0.1:6379>
127.0.0.1:6379>


[ view entry ] ( 1355 views )   |  print article

| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |


2024 By Angel Cool