Basic Command Used in String Data Structure in Redis
1. SET
This command will store a string value.
Syntax:
SET Key Value
Example:
SET User:1 GFG
Time Complexity: O(1)
2. SETNX
This command will stores a string value only if the key doesn’t already exist. Useful for implementing locks.
Syntax:
SETNX key value
Example:
SETNX key “Hello”
Time Complexity: O(1)
3. GET
This command will retrieves a string value.
Syntax:
GET key
Example:
GET user:123:name
Time Complexity: O(1)
4. MSET
This command will stores multiple values in just a command.
Syntax:
MSET key value [key value …]
Example:
MSET key1 “Hello” key2 “World”
Time Complexity: O(N)
5. MGET
This command retrieves multiple string values in a single operation.
Syntax:
MGET key [key …]
Example:
MGET key1 key2
Time complexity: O(N)
6. INCR
This command will parse a string to integer, increment by one.
Syntax:
INCR key
Example:
INCR mykey
Time complexity: O(1)
7. APPEND
This command will use to append a value to an existing string.
Syntax:
APPEND key value
Example:
APPEND mykey “Hello”
Time complexity: O(1)
8. GETRANGE
This command will use to print the substring of a string.
Syntax:
GETRANGE key start end
Example:
GETRANGE mykey 0 3
Time complexity: O(N)
9. GETSET
This command will sets the string value of a string and returns its old value.
Syntax:
GETSET key value
Example:
GETSET mycounter “0”
Time complexity: O(1)
10. GETBIT
This command will set the string value of a key and return its old value.
Syntax:
GETBIT key offset
Example:
GETBIT mykey 0
Time complexity: O(1)
11. STRLEN
This command will return the length of the string.
Syntax:
STRLEN key
Example:
STRLEN mykey
Time complexity: O(1)
12. DECR
This command will parse a string to integer, decrease by one.
Syntax:
DECR key
Example:
DECR mykey
Time complexity: O(1)
13. INCRBY
This command will Increments the integer value of a key by the given amount.
Syntax:
INCRBY key increment
Example:
INCRBY mykey 5
Time complexity: O(1)
14. INCRBYFLOAT
This command will Increments the float value of a key by the given amount.
Syntax:
INCRBYFLOAT key increment
Example:
INCRBYFLOAT mykey 0.1
Time complexity: O(1)
15. DECRBY
This command will Decrements the integer value of a key by the given number
Syntax:
DECRBY key decrement
Example:
DECRBY mykey 3
Time complexity: O(1)
16. MSETNX
This command will set multiple keys to multiple values, only if none of the keys exist.
Syntax:
MSETNX key value [key value …]
Example:
MSETNX key1 “Hello” key2 “there”
Time complexity: O(N)
17. SETEX
This command will sets the value with the expiry of a key.
Syntax:
SETEX key seconds value
Example:
SETEX mykey 10 “Hello”
Time complexity: O(1)
18. SETNX
This command will set the value of a key, only if the key does not exist.
Syntax:
SETNX key value
Example:
SETNX mykey “World”
Time complexity: O(1)
19. SETRANGE
This command will Overwrites the part of a string at the key starting at the specified offset.
Syntax:
SETRANGE key offset value
Example:
SETRANGE key1 6 “Redis”
Time complexity: O(1)
20. PSETEX
This command will sets the value and expiration in milliseconds of a key.
Syntax:
PSETEX key milliseconds value
Example:
PSETEX mykey 1000 “Hello”
Time complexity: O(1)
Redis strings typically have a size limit of 512 MB.
Example of Redis String:
Example1:
MSET user: 1 “GFG” user: 2 “Noida” user: 3 “UP”
MGET user:1 user: 2 user: 3Output: GFG
Noida
UP
Example 2:
SET user:1 “GFG”
APPEND user: 1 “isTheBest”
GET user: 1Output: GFGisTheBest
Complete Guide on Redis Strings
Redis String is a sequence of bytes that can store a sequence of bytes, including text, Object, and binary arrays. which can store a maximum of 512 megabytes in one string. Redis String can also be used like a Redis Key for mapping a string to another string. String Data Types are useful in different types of use cases like caching HTML fragments or different pages.
Important Topics on Redis Strings
- SET and GET Command
- Basic Command Used in String Data Structure in Redis:
- Redis Strings as Counters
- How to manage counters?
- Performance
- Alternatives of String in Redis
Contact Us