Redis

redis-cli

Debian based installation

sudo apt-get install redis-tools

Helpful flags

--help : the help
-p PORT : specify the port (default: 6379)
-h HOST : specify the host (default: 127.0.0.1)

redis and Docker

Run Redis with Docker :

$ docker run -p 6379:6379 -v /c/Users/redis.conf:/usr/local/etc/redis/redis.conf --name myredis redis redis-server /usr/local/etc/redis/redis.conf

Things to note :

– To export the container externally : defining an external port is required, that is -p 6379:6379

– Specifying a redis.conf file may be helpful and some default values may also prevent client to communicate with the Redis server.
You can find a default redis.conf here for the last version.
Things to probably change in the conf file :
bind 127.0.0.1 may annoy you. You may want to comment it to allow any host to connect to the DB.
protected-mode yes may also annoy you. You can change the value to no to allow the communication without restriction : any host allowed and no password.
For testing or in local/dev environments these low secure changes may make sense.

– the /c/Users path is needed because of volumes restrictions on the directories with Docker on Windows. For other OS, no such constraints.

Run Redis client from the running container.

docker exec -it containerID redis-cli

Run Redis in persistent mode (append-only flag):

docker run --name some-redis -d -v redis_vol:/data redis redis-server --appendonly yes

With the appendonly mode enabled the /data volume is created in the container. So we could map it on the host to reuse it easily.

To test the persistence we could add an entry :
docker exec -ti some-redis redis-cli set dog snoopy Then we remove the container :
docker rm -f some-redis
And then we restart that and we request the entry : docker exec -ti some-redis redis-cli get dog


User interface for Redis : redis-commander.

It runs on NodeJS.

Install :
npm install -g redis-commander

Run :
redis-commander --cc --redis-port 6379 --redis-host 192.168.99.100

Note that the -cc flag may be your savior (that means « clear cache ») : indeed redis-commander caches properties and seems to not consider in any case new properties submitted from the command line.

—————————————————————-

REDIS commands

Redis Server configuration commands

Read a server config property value :
config get fooProperty
We can also use wildcards :
config get *max-*-entries*
or even :
config get *
Basic reading example :
config get appendonly
config get save

Update a server config property value (at runtime) :
config set fooProperty fooValue

List keys and metadata commands

List all keys :
KEYS *

List keys matching a wildcarded expression:
KEYS foo*bar:*

Warn : that blocks the server.

SCAN
That doesn’t block the server.

Query the type of a key :
TYPE foo_key

Query the TTL of a key :
TTL foo_key

Set and Get values according to the datetype

String datatype

Set (add or replace) a key (String value) :
SET key value

Get a value for a specific key :
GET key

List datatype

todo…

Set datatype

Set is an unordered collection of Strings. It is possible to add, remove, and test for existence of members in O(1) (constant time regardless of the number of elements contained inside the Set).

Add member(s) to a Set :
SADD FOO_SET member1, member2, ...

List members of a Set :
SMEMBERS FOO_SET

Is a value is member of a Set :
SISMEMBER FOO_SET FOO_VALUE

Hash datatype

Hash is  a map of string values by string fields . They allows to represent objects (e.g. A User with a number of fields like name, surname, age, and so forth)

Sets field + value for a specific key :
Syntax : hset KEY FIELD1 VALUE1 [FIELD2 VALUE2...]
Ex:
hset user-1 name "david" genre "male"

List fieldnames for a specific key :
Syntax : hkeys KEY

List all fields-values for a specific key :
Syntax : hgetall KEY
Ex:
hgetall user-1
It lists fields-values in the form :

1)fooField
2)value of fooField
3)barField
4)value of fooBar

Get the value associated to a field for a specific key :
Syntax : hget KEY FIELD
Ex:
hget user-1 genre

delete the whole hash:
Syntax : del KEY
Ex:
del family

delete specific field(s) for a specific key :
Syntax : hdel KEY FIELDS
Ex:
hdel family childOne ChildTwo

Sorted sets datatype

Sorted Sets are similarly to Redis Sets, non repeating collections of Strings.
The difference : every member of a Sorted Set is associated with a score that is used to order the set : from the smallest to the greatest score. While members are unique, scores may be repeated.

Adds one or several members with the specified scores for a specific key. :
Syntax : zadd KEY SCORE MEMBER [SCORE MEMBER...]
Ex:
zadd zset-foo-key 5000 member1
zadd zset-foo-key 2000 member2

List members (and score optionally) of the specified ranged (the range is always related to score) :
Syntax : zrange zset-foo-key START STOP [WITHSCORES]
Ex: Display all members with their score
zrange zset-foo-key 0 -1 WITHSCORES

Ex: Display first member
zrange zset-foo-key 0 0

Deletion

Delete a key :
DEL fooKey

Delete a set of keys :
There is not native solution. So a way is using shell features:
redis-cli KEYS foo:* | xargs redis-cli DEL {}

Ce contenu a été publié dans Non classé. Vous pouvez le mettre en favoris avec ce permalien.

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *