Outils pour utilisateurs

Outils du site


bases_de_donnees:redis

Ceci est une ancienne révision du document !


Redis

Application pour gérer Redis: RedisInsight

Commands

Utilisation sans docker

sudo apt install redis
redis-server  # starts redis

Utilisation du CLI:

redis-cli

Commands

  • Basic values (string): SET, GET
  • HashMaps: HSET, HGET, HMGET (Get multiple keys)

Keys and Scan

KEYS somekey:*

L'utilisation de KEYS peut ne pas être recommendé.

SCAN 0 COUNT 50 MATCH somekey:*

HashMaps

Example:

> HSET loggedUser:123 name "John Doe" url "https://url" role "Developer"
(integer) 3
> HMGET loggedUser:123 name role
1) "John Doe"
2) "Developer"

Exemple d'un panier:

> HSET cart:123 prod:1:name "Oranges" prod:1:price "2,23" prod:1:amount "1" prod:2:name "Apples" prod:2:price "1,50" prod:2:amount "4"
(integer) 5
> HINCRBY cart:123 prod:1:amount 3
(integer) 4
> HGETALL cart:123
 1) "prod:1:name"
 2) "Oranges"
 3) "prod:1:price"
 4) "1,50"
 5) "prod:1:amount"
 6) "4"
 7) "prod:2:name"
 8) "Apples"
 9) "prod:2:amount"
10) "4"
> HLEN cart:123
(integer) 5

Listes

  • LPUSH: Left push
  • RPUSH: Right push
> LPUSH mylist "one" "two" "three" "last element"
> LPOP mylist
"last element"
(integer) 4
> LPOP mylist 3
1) "three"
2) "two"
3) "one"
  • FIFO Queue: using RPUSH with LPOP
  • LIFO Queue: using LPUSH with LPOP
> LPUSH mylist "one" "two" "three" "last element"
(integer) 4
> LRANGE mylist 0 1
1) "last element"
2) "three"
> LRANGE mylist 0 -1
1) "last element"
2) "three"
3) "two"
4) "one"

INSERT

> LINSERT mylist AFTER "two" "four"
(integer) 5
> LRANGE mylist 0 -1
1) "last element"
2) "three"
3) "two"
4) "four"
5) "one"

Sets

Set est une liste non ordonnée.

  • SADD: Add to a set
  • SPOP: Retrieve random element from set
  • SREM: Remove element from set
  • SMEMBERS [queuename]: list elements
SADD thequeue "Element1" "Element2" "Element2"
SMEMBERS thequeue

Utilisation comme Message Broker

SUBSCRIBE <channel name>

Sur un client Consumer:

SUBSCRIBE notifications

Sur un autre client Producer:

PUBLISH notifications "Ici un message important"

Le message apparaîtra sur le client subcriber.

bases_de_donnees/redis.1685494951.txt.gz · Dernière modification : 2023/05/31 03:02 de sgariepy