serhii.net

In the middle of the desert you can say anything you want

29 Nov 2022

Redis basics

  • Links:
  • Basics:
    • Installed through apt-get
    • after that, redis-cli set test 1 etc. immediately work - did it start a server in the background?
      • Ah, it then becomes a service that I can systemctl disable redis-cli etc!
    • Without arguments, redis-cli starts in interactive mode!
      • That has nice hints a la fish shell!
  • Transactios:
    > r
    127.0.0.1:6379> multi
    OK
    127.0.0.1:6379> get google
    QUEUED
    127.0.0.1:6379> incr google_accesses
    QUEUED
    127.0.0.1:6379> exec
    1) "http://google.com"
    2) (integer) 1
    127.0.0.1:6379>
    
  • Help:
    • help <Tab> autocompletes
    • help @hash

Data structures

Hashes:

# Create a hashset that has field f1 w/ value v1 etc.:
127.0.0.1:6379> hmset myhash f1 v1 f2 v2
OK
127.0.0.1:6379> hgetall myhash
1) "f1"
2) "v1"
3) "f2"
4) "v2"
127.0.0.1:6379> hget myhash f1
"v1"

Operations on hashes:

# We create a hset s_google that has an url and accesses counter
127.0.0.1:6379> hset s_google url url_google accesses 0
(integer) 2
127.0.0.1:6379> hmget s_google url accesses
1) "url_google"
2) "0"
# Increase accesses by 1
127.0.0.1:6379> HINCRBY s_google accesses 1
(integer) 1
127.0.0.1:6379> hmget s_google url accesses
1) "url_google"
2) "1"

Deleting stuff

  • DEL key
  • FLUSHALL to delete everything

Using files

  • cat file.txt | redis-cli --pipe

Sorted sets

127.0.0.1:6379> zadd myss 1 'one' 2 'two'
(integer) 2
127.0.0.1:6379> ZSCORE myss 'one'
"1"
127.0.0.1:6379> ZSCORE myss 'one'
127.0.0.1:6379> get B
"https://www.wikipedia.org"
127.0.0.1:6379> get A
"http://www.openstreetmap.org"
127.0.0.1:6379> ZCARD accesses
(integer) 2
127.0.0.1:6379> ZCARD accesses
(integer) 2
127.0.0.1:6379> ZRANGE accesses 0 40
1) "A"
2) "B"
127.0.0.1:6379> ZRANGE accesses 0 40 withscores
1) "A"
2) "1"
3) "B"
4) "1"
127.0.0.1:6379>
Nel mezzo del deserto posso dire tutto quello che voglio.