In this section, we will delve into some of the more advanced commands available in Redis. These commands will help you perform complex operations and manage your Redis data more effectively. We will cover the following advanced commands:

  1. SCAN, SSCAN, HSCAN, ZSCAN
  2. BITFIELD
  3. GEOADD, GEODIST, GEORADIUS
  4. PUB/SUB

  1. SCAN, SSCAN, HSCAN, ZSCAN

SCAN

The SCAN command is used to incrementally iterate over a collection of elements. Unlike KEYS, which returns all keys in a single call, SCAN allows you to iterate through the keys in a non-blocking manner.

Syntax:

SCAN cursor [MATCH pattern] [COUNT count]

Example:

127.0.0.1:6379> SCAN 0 MATCH user:* COUNT 10
1) "10"
2) 1) "user:1"
   2) "user:2"
   3) "user:3"

SSCAN

The SSCAN command is used to incrementally iterate over the elements in a set.

Syntax:

SSCAN key cursor [MATCH pattern] [COUNT count]

Example:

127.0.0.1:6379> SADD myset "one" "two" "three"
127.0.0.1:6379> SSCAN myset 0 MATCH t*
1) "0"
2) 1) "two"
   2) "three"

HSCAN

The HSCAN command is used to incrementally iterate over the fields and values of a hash.

Syntax:

HSCAN key cursor [MATCH pattern] [COUNT count]

Example:

127.0.0.1:6379> HSET myhash field1 "Hello" field2 "World"
127.0.0.1:6379> HSCAN myhash 0 MATCH f*
1) "0"
2) 1) "field1"
   2) "Hello"
   3) "field2"
   4) "World"

ZSCAN

The ZSCAN command is used to incrementally iterate over the elements in a sorted set.

Syntax:

ZSCAN key cursor [MATCH pattern] [COUNT count]

Example:

127.0.0.1:6379> ZADD myzset 1 "one" 2 "two" 3 "three"
127.0.0.1:6379> ZSCAN myzset 0 MATCH t*
1) "0"
2) 1) "two"
   2) "3"
   3) "three"
   4) "2"

  1. BITFIELD

The BITFIELD command treats a string as an array of bits and allows you to perform bit-level operations on it.

Syntax:

BITFIELD key [GET type offset] [SET type offset value] [INCRBY type offset increment]

Example:

127.0.0.1:6379> BITFIELD mykey SET i8 100 1
1) 0
127.0.0.1:6379> BITFIELD mykey GET u4 100
1) 1

  1. GEOADD, GEODIST, GEORADIUS

GEOADD

The GEOADD command adds one or more geospatial items (latitude, longitude, name) to the specified key.

Syntax:

GEOADD key longitude latitude member [longitude latitude member ...]

Example:

127.0.0.1:6379> GEOADD mygeo 13.361389 38.115556 "Palermo"
127.0.0.1:6379> GEOADD mygeo 15.087269 37.502669 "Catania"

GEODIST

The GEODIST command returns the distance between two members of a geospatial index.

Syntax:

GEODIST key member1 member2 [unit]

Example:

127.0.0.1:6379> GEODIST mygeo Palermo Catania km
"166.2742"

GEORADIUS

The GEORADIUS command returns the members of a geospatial index within the specified radius.

Syntax:

GEORADIUS key longitude latitude radius unit [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count] [ASC|DESC] [STORE key] [STOREDIST key]

Example:

127.0.0.1:6379> GEORADIUS mygeo 15 37 200 km WITHDIST
1) 1) "Catania"
   2) "56.4413"
2) 1) "Palermo"
   2) "190.4424"

  1. PUB/SUB

PUBLISH

The PUBLISH command posts a message to the given channel.

Syntax:

PUBLISH channel message

Example:

127.0.0.1:6379> PUBLISH mychannel "Hello, World!"
(integer) 1

SUBSCRIBE

The SUBSCRIBE command listens for messages published to the given channels.

Syntax:

SUBSCRIBE channel [channel ...]

Example:

127.0.0.1:6379> SUBSCRIBE mychannel
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "mychannel"
3) (integer) 1
1) "message"
2) "mychannel"
3) "Hello, World!"

Practical Exercises

Exercise 1: Using SCAN to Find Keys

  1. Add several keys to your Redis instance with the prefix test:.
  2. Use the SCAN command to find all keys with the prefix test:.

Solution:

127.0.0.1:6379> SET test:1 "value1"
127.0.0.1:6379> SET test:2 "value2"
127.0.0.1:6379> SCAN 0 MATCH test:*
1) "0"
2) 1) "test:1"
   2) "test:2"

Exercise 2: Using GEOADD and GEODIST

  1. Add three locations to a geospatial index.
  2. Calculate the distance between two of the locations.

Solution:

127.0.0.1:6379> GEOADD mygeo 13.361389 38.115556 "Palermo"
127.0.0.1:6379> GEOADD mygeo 15.087269 37.502669 "Catania"
127.0.0.1:6379> GEOADD mygeo 12.496365 41.902783 "Rome"
127.0.0.1:6379> GEODIST mygeo Palermo Catania km
"166.2742"

Exercise 3: Using PUB/SUB

  1. Open two Redis CLI sessions.
  2. In the first session, subscribe to a channel.
  3. In the second session, publish a message to that channel.

Solution:

# Session 1
127.0.0.1:6379> SUBSCRIBE mychannel

# Session 2
127.0.0.1:6379> PUBLISH mychannel "Hello, Redis!"

Conclusion

In this section, we explored some of the advanced commands in Redis, including SCAN, BITFIELD, geospatial commands, and PUB/SUB. These commands allow you to perform more complex operations and manage your data more effectively. Make sure to practice these commands to become proficient in using Redis for advanced use cases. In the next module, we will dive into Redis transactions and pipelining to further enhance your Redis skills.

© Copyright 2024. All rights reserved