You are currently viewing Redis Commands Tutorial with Examples

Redis Commands Tutorial with Examples

  • Post author:
  • Post category:Redis
  • Post comments:0 Comments
  • Post last modified:March 31, 2024

Redis is an open-source, in-memory data structure store used as a database, cache, and message broker. It supports various data structures such as strings, hashes, lists, sets, and more. In this tutorial, we’ll cover some commonly used Redis commands with examples.

Installing Redis

Before we get started, ensure you have Redis installed. You can install it on Linux using:

sudo apt update
sudo apt install redis-server

For other operating systems, you can follow the official installation guide.

Connecting to Redis

After installing Redis, you can connect to it using the redis-cli command:

redis-cli

This will open the Redis command-line interface where you can execute various commands.

Common Redis Commands

1. SET and GET

The SET command sets the value of a key, and GET retrieves the value of a key.

SET mykey "Hello Redis"
GET mykey

Output:

"OK"
"Hello Redis"

2. KEYS

The KEYS command is used to list all keys matching a pattern.

SET user:1:name "Alice"
SET user:2:name "Bob"
KEYS user:*

Output:

1) "user:1:name"
2) "user:2:name"

3. DEL

The DEL command is used to delete a key.

DEL mykey

4. INCR and DECR

INCR increments the integer value of a key by one, and DECR decrements it by one.

SET counter 10
INCR counter
DECR counter

Output:

(integer) 11
(integer) 10

5. EXPIRE

EXPIRE sets a key’s time to live in seconds.

SET tempkey "I will expire"
EXPIRE tempkey 30

6. LPUSH and LRANGE

LPUSH adds an element to the head of a list, and LRANGE retrieves elements from the list.

LPUSH mylist "one"
LPUSH mylist "two"
LPUSH mylist "three"
LRANGE mylist 0 -1

Output:

1) "three"
2) "two"
3) "one"

7. HMSET and HGETALL

HMSET sets multiple fields of a hash, and HGETALL retrieves all fields and values of a hash.

HMSET user:1 name "Alice" age 30
HGETALL user:1

Output:

1) "name"
2) "Alice"
3) "age"
4) "30"

8. SADD and SMEMBERS

SADD adds members to a set, and SMEMBERS retrieves all members of a set.

SADD myset "one"
SADD myset "two"
SADD myset "three"
SMEMBERS myset

Output:

1) "one"
2) "two"
3) "three"

9. ZADD and ZRANGE

ZADD adds members with scores to a sorted set, and ZRANGE retrieves elements in a sorted set by index.

ZADD myzset 1 "one"
ZADD myzset 2 "two"
ZADD myzset 3 "three"
ZRANGE myzset 0 -1 WITHSCORES

Output:

1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"

10. FLUSHALL

FLUSHALL deletes all keys from all databases.

FLUSHALL

These are just a few of the many commands available in Redis. For a complete list of commands and their usage, refer to the Redis Command Reference.

Conclusion

Redis is a powerful tool with a wide range of commands for managing various data structures. In this tutorial, we covered some basic and commonly used commands. Remember to always handle sensitive data with care, especially in production environments. For more advanced usage and features, refer to the Redis Documentation.

Learn More

Sure, here’s a list of 50 URLs related to Redis commands tutorials and resources:

  1. Redis Commands
  2. Redis Commands Tutorial on Tutorialspoint
  3. Redis Commands Cheat Sheet
  4. Redis Commands on RedisGreen
  5. Redis Commands with Examples on GeeksforGeeks
  6. Redis Commands Explained on Redis.io Blog
  7. Redis Command Line Tutorial on YouTube
  8. Redis Commands – An Overview
  9. Redis Commands and Operations
  10. Redis Commands Tutorial on Guru99
  11. Redis Command Reference
  12. Redis Commands with Examples on Programiz
  13. Redis Command Reference
  14. Redis Commands Documentation on Baeldung
  15. Redis Commands on DigitalOcean
  16. Redis Commands Tutorial on W3schools
  17. Redis Commands with Examples on Redis.io
  18. Redis Commands and Data Types
  19. Redis Commands with Examples on Edureka
  20. Redis Commands Quick Reference on GitHub
  21. Redis Commands and Data Types on JavaTpoint
  22. Redis Commands Overview on Tutorialbrain
  23. Redis Commands on DataFlair
  24. Redis Commands and Operations on Sanfoundry
  25. Redis Commands Cheat Sheet on LeetCode
  26. Redis Commands Tutorial on LinuxHint
  27. Redis Commands and Examples on Redis.io
  28. Redis Commands and Use Cases on InfoQ
  29. Redis Commands and Data Structures
  30. Redis Commands with Syntax on Fresh2Refresh
  31. Redis Commands and Tutorial on JournalDev
  32. Redis Commands and Introduction on ZetCode
  33. Redis Commands Explained with Examples on Browserling
  34. Redis Commands and Data Types on Codecademy
  35. Redis Commands Tutorial on Studytonight
  36. Redis Commands and Redis Data Types on TutorialsTeacher
  37. Redis Commands Overview on RedisGreen
  38. Redis Commands and Examples on Spring Framework Guru
  39. Redis Commands Tutorial on RedisGreen Blog
  40. Redis Commands and Transactions
  41. Redis Commands and Pipelining on CodeProject
  42. Redis Commands and Keys
  43. Redis Commands on Redis.io Documentation
  44. Redis Commands and Redisson
  45. Redis Commands and Lua Scripting
  46. Redis Commands Tutorial on Learning Journal
  47. Redis Commands with Real-Life Examples
  48. Redis Commands and Pub/Sub
  49. Redis Commands and HyperLogLog
  50. Redis Commands on Codecademy

Leave a Reply