Contenido del Curso
Introduction to Redis
Introduction to Redis
String
We've already covered basic Redis commands like SET
, GET
, and DEL
, which allow you to work with keys and their values. These commands primarily deal with strings.
Now, let's explore advanced string commands that provide additional functionality.
Advanced String Commands
Redis provides a range of advanced commands to enhance string manipulation. These commands allow you to perform conditional operations, work with multiple keys at once, and modify existing values efficiently.
SETNX
The SETNX
command (Set if Not Exists) sets a value only if the key does not already exist. If the key exists, the command will make no changes. This command is useful when you want to avoid overwriting existing values.
If mykey
does not exist, it will be created and assigned the value "value"
. If the key already exists, its value will remain unchanged.
STRLEN
The STRLEN
command returns the length of the string associated with a given key. It provides the number of characters in the string.
If mykey
exists and its value is a string, Redis will return its length.
MSET and MGET
The MSET
command allows you to set multiple keys and their values in a single operation. All keys will be updated, and if any of them already exist, their values will be overwritten.
In this example, three keys— key1
, key2
, and key3
are set with their respective values.
The MGET
command retrieves the values of multiple keys in a single operation. It returns the values as a list.
This command will return the values of all three keys. If a key does not exist, its value will be nil
.
APPEND
The APPEND
command adds data to the end of the string associated with a given key. If the key does not exist, Redis creates it with the specified value.
If mykey
exists, the string "additional value"
will be appended to its current value. If the key does not exist, it will be created with the value "additional value"
.
1. Which Redis command sets a value only if the key does not already exist?
2. Which Redis command retrieves the length of the string associated with a key?
¡Gracias por tus comentarios!