top of page
Writer's pictureSiddhesh Kadam

Redis String

A Redis string is a sequence of bytes. This means that it can store any type of data, including text, serialized objects, and binary arrays. Redis strings are the most basic and versatile data type in Redis.


1. Size Limitation

A single Redis string can be a maximum of 512MB


2. How can I see if a Redis string name already exists ?


Using EXIST we can determine if a key exists in Redis.

127.0.0.1:6379> EXISTS  teststringkey
(integer) 0
127.0.0.1:6379>

The output is 0 means key teststringkey is not present in Redis. You will receive an output similar to the one below if you execute the same command on a key that is present in Redis. You can see that the output is now 1 here.

127.0.0.1:6379> EXISTS  testdatakey
(integer) 1
127.0.0.1:6379>

3. Create a string

The SET command in Redis can be used to create a string. The string value and the key name are the two arguments that the SET command accepts.

127.0.0.1:6379> SET teststringkey inventory1
OK
127.0.0.1:6379>

4. Get a string content

The GET command can be used to retrieve a string from Redis. The value of the string, or nil if the key is not present, is returned by the GET command, which accepts the key name as an argument.

127.0.0.1:6379> GET teststringkey
"inventory1"
127.0.0.1:6379>

5. Append content to a Redis string


The APPEND command can be used to append content to a Redis string. The name of the key and the value of the string to append are the two arguments required by the APPEND command.

127.0.0.1:6379> APPEND teststringkey " inventory2"
(integer) 21
127.0.0.1:6379> GET teststringkey
"inventory1 inventory2"
127.0.0.1:6379>

6. Get a substring of the string


The GETRANGE command in Redis can be used to obtain a substring of a string. Three arguments are required for the GETRANGE command: the key name, the start offset, and the end offset. The index of the substring's first character is the start offset, and the index of the substring's last character is the end offset.

127.0.0.1:6379> GETRANGE teststringkey 0 5
"invent"
127.0.0.1:6379>

As you can see from the example above, I specified the start offset as 0 and the end offset as 5, giving me the first six characters of the string. 7. Get the values of all the given string keys

The MGET command can be used to retrieve the values of all the given keys in Redis. The MGET command accepts a list of key names as an argument and returns a list of string values, or nil if no keys are found.

Let us make another string key called teststringkey2.

127.0.0.1:6379> set teststringkey2 server1
OK
127.0.0.1:6379>
127.0.0.1:6379> MGET teststringkey teststringkey2
1) "inventory1 inventory2"
2) "server1"
127.0.0.1:6379>

8. Overwrite part of a string at key starting at the specified offset The SETRANGE command in Redis can be used to overwrite a portion of a string at key beginning at the specified offset. The SETRANGE command takes three arguments: the key name, the start offset, and the string to overwrite.

127.0.0.1:6379> get teststringkey
"inventory1 inventory2"
127.0.0.1:6379>
127.0.0.1:6379> SETRANGE teststringkey 11 inventory5
(integer) 21
127.0.0.1:6379> get teststringkey
"inventory1 inventory5"
127.0.0.1:6379>

In the example above, we replaced the string content "inventory2" with "inventory5" by specifying a start offset of 11.


9. Size of Redis string.

In Redis, the STRLEN command is used to determine the length of the string value stored at the key. It returns the length of the string as an integer, or 0 if the key does not exist.

127.0.0.1:6379> STRLEN teststringkey
(integer) 21
127.0.0.1:6379>

10. Memory usage of Redis string.


In Redis, the MEMORY USAGE command is used to retrieve the memory usage of the key specified in the argument. It returns an integer value representing the number of bytes required to store the key and its value in RAM.

127.0.0.1:6379> MEMORY USAGE teststringkey
(integer) 104
127.0.0.1:6379>

Comentários

Avaliado com 0 de 5 estrelas.
Ainda sem avaliações

Adicione uma avaliação
bottom of page