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:
- SCAN, SSCAN, HSCAN, ZSCAN
- BITFIELD
- GEOADD, GEODIST, GEORADIUS
- PUB/SUB
- 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:
Example:
SSCAN
The SSCAN
command is used to incrementally iterate over the elements in a set.
Syntax:
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:
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:
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"
- BITFIELD
The BITFIELD
command treats a string as an array of bits and allows you to perform bit-level operations on it.
Syntax:
Example:
- GEOADD, GEODIST, GEORADIUS
GEOADD
The GEOADD
command adds one or more geospatial items (latitude, longitude, name) to the specified key.
Syntax:
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:
Example:
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"
- PUB/SUB
PUBLISH
The PUBLISH
command posts a message to the given channel.
Syntax:
Example:
SUBSCRIBE
The SUBSCRIBE
command listens for messages published to the given channels.
Syntax:
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
- Add several keys to your Redis instance with the prefix
test:
. - Use the
SCAN
command to find all keys with the prefixtest:
.
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
- Add three locations to a geospatial index.
- 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
- Open two Redis CLI sessions.
- In the first session, subscribe to a channel.
- 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.
Redis Course
Module 1: Introduction to Redis
Module 2: Redis Data Structures
Module 3: Redis Commands and Operations
Module 4: Redis Persistence
Module 5: Redis Security
Module 6: Redis Performance Optimization
Module 7: Redis Clustering and High Availability
Module 8: Redis Modules and Extensions
- Introduction to Redis Modules
- Popular Redis Modules
- Creating Custom Modules
- Using Redis with Other Technologies