In this section, we will cover the fundamental commands in Redis that are essential for interacting with the database. These commands will help you perform basic operations such as setting and getting values, deleting keys, and checking the existence of keys.

Key Concepts

  1. Keys: Unique identifiers for storing data in Redis.
  2. Values: The data associated with keys, which can be of various data types (strings, lists, sets, etc.).
  3. Commands: Instructions given to Redis to perform operations on keys and values.

Common Redis Commands

  1. SET and GET

The SET command is used to store a value associated with a key, and the GET command retrieves the value of a key.

# Syntax
SET key value
GET key

Example:

SET mykey "Hello, Redis!"
GET mykey

Explanation:

  • SET mykey "Hello, Redis!": Stores the string "Hello, Redis!" with the key mykey.
  • GET mykey: Retrieves the value associated with mykey, which is "Hello, Redis!".

  1. DEL

The DEL command is used to delete one or more keys.

# Syntax
DEL key [key ...]

Example:

DEL mykey

Explanation:

  • DEL mykey: Deletes the key mykey and its associated value.

  1. EXISTS

The EXISTS command checks if a key exists in the database.

# Syntax
EXISTS key

Example:

EXISTS mykey

Explanation:

  • EXISTS mykey: Returns 1 if the key mykey exists, otherwise returns 0.

  1. INCR and DECR

The INCR and DECR commands increment and decrement the integer value of a key by one, respectively.

# Syntax
INCR key
DECR key

Example:

SET counter 10
INCR counter
DECR counter

Explanation:

  • SET counter 10: Sets the value of counter to 10.
  • INCR counter: Increments the value of counter by 1, resulting in 11.
  • DECR counter: Decrements the value of counter by 1, resulting in 10.

  1. MSET and MGET

The MSET command sets multiple keys to multiple values, and the MGET command retrieves the values of multiple keys.

# Syntax
MSET key value [key value ...]
MGET key [key ...]

Example:

MSET key1 "value1" key2 "value2"
MGET key1 key2

Explanation:

  • MSET key1 "value1" key2 "value2": Sets key1 to "value1" and key2 to "value2".
  • MGET key1 key2: Retrieves the values of key1 and key2, which are "value1" and "value2", respectively.

  1. EXPIRE

The EXPIRE command sets a timeout on a key. After the timeout, the key will be automatically deleted.

# Syntax
EXPIRE key seconds

Example:

SET mykey "Hello, Redis!"
EXPIRE mykey 10

Explanation:

  • SET mykey "Hello, Redis!": Stores the string "Hello, Redis!" with the key mykey.
  • EXPIRE mykey 10: Sets a timeout of 10 seconds on mykey. After 10 seconds, mykey will be deleted.

Practical Exercises

Exercise 1: Basic Key-Value Operations

  1. Set a key name with the value "Alice".
  2. Retrieve the value of the key name.
  3. Delete the key name.
  4. Check if the key name exists.

Solution:

SET name "Alice"
GET name
DEL name
EXISTS name

Exercise 2: Increment and Decrement

  1. Set a key score with the value 5.
  2. Increment the value of score by 1.
  3. Decrement the value of score by 1.

Solution:

SET score 5
INCR score
DECR score

Exercise 3: Multiple Keys and Values

  1. Set multiple keys key1 and key2 with values "value1" and "value2".
  2. Retrieve the values of key1 and key2.

Solution:

MSET key1 "value1" key2 "value2"
MGET key1 key2

Common Mistakes and Tips

  • Mistake: Forgetting to use quotes for string values.

    • Tip: Always use quotes for string values to avoid syntax errors.
  • Mistake: Using GET on a non-existent key.

    • Tip: Use EXISTS to check if a key exists before using GET.

Conclusion

In this section, we covered the basic commands in Redis, including setting and getting values, deleting keys, checking key existence, and more. These commands form the foundation for interacting with Redis and are essential for performing basic operations. In the next section, we will delve into more advanced commands and operations.

© Copyright 2024. All rights reserved