top of page

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.> EXISTS  teststringkey
(integer) 0>

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.> EXISTS  testdatakey
(integer) 1>

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.> SET teststringkey inventory1

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.> GET teststringkey

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.> APPEND teststringkey " inventory2"
(integer) 21> GET teststringkey
"inventory1 inventory2">

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.> GETRANGE teststringkey 0 5

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.> set teststringkey2 server1
OK>> MGET teststringkey teststringkey2
1) "inventory1 inventory2"
2) "server1">

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.> get teststringkey
"inventory1 inventory2">> SETRANGE teststringkey 11 inventory5
(integer) 21> get teststringkey
"inventory1 inventory5">

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.> STRLEN teststringkey
(integer) 21>

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.> MEMORY USAGE teststringkey
(integer) 104>



bottom of page