Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära String | Datatyper i Redis
Introduktion till Redis

String

Svep för att visa menyn

Note
Definition

The simplest form of data that can be stored in Redis is a string. Strings in Redis can hold simple values or more complex objects converted into a string format.

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.

SETNX mykey "value"

If mykey does not exist, it will be created and assigned the value "value". If the key already exists, its value will remain unchanged.

setnx

STRLEN

The STRLEN command returns the length of the string associated with a given key. It provides the number of characters in the string.

STRLEN mykey

If mykey exists and its value is a string, Redis will return its length.

strlen

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.

MSET key1 "value1" key2 "value2" key3 "value3"

In this example, three keyskey1, 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.

MGET key1 key2 key3

This command will return the values of all three keys. If a key does not exist, its value will be nil.

mset+mget

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.

APPEND mykey "additional 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".

append

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?

question mark

Which Redis command sets a value only if the key does not already exist?

Vänligen välj det korrekta svaret

question mark

Which Redis command retrieves the length of the string associated with a key?

Vänligen välj det korrekta svaret

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 1

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Avsnitt 3. Kapitel 1
some-alt