Data Types and Common Commands in Redis
String Operations
Common commands for string operations in Redis:
SET key value: Sets the value of a specified key.- Example:
127.0.0.1:6379> set age 20 - The value set using the
SETcommand is always stored as a string. - If the key already exists, the value will be updated; if the key does not exist, a new key-value pair will be created.
- Example:
GET key: Retrieves the value of a specified key.- Example:
127.0.0.1:6379> get age
- Example:
SETEX key seconds value: Sets the value of a specified key and its expiration time in seconds (commonly used for storing verification codes).SETNX key value: Sets the value of a key only if the key does not already exist (commonly used for distributed locks).
Hash Operations
Redis hashes are mapping tables of string-type fields and values, particularly suitable for storing objects.
| key | field1 | value1 |
|---|---|---|
| field2 | value2 |
Common commands:
HSET key field value: Sets the value of a field in the hash stored at a key.HGET key field: Retrieves the value of a specified field in a hash.HDEL key field: Deletes a specified field from a hash.HKEYS key: Retrieves all fields in a hash (returns all fields of the specified key).HVALS key: Retrieves all values in a hash.HGETALL key: Retrieves all fields and values in a hash.
List Operations
Redis lists are simple string lists sorted by insertion order (ordered and can contain duplicates).
| Key | a | b | c | d |
|---|
Common commands:
LPUSH key value1 [value2]: Pushes one or more values onto the head of a list.LRANGE key start stop: Retrieves elements within a specified range of a list. Use-1for the last element (e.g.,LRANGE key 0 -1).RPOP key: Removes and retrieves the last element in a list.LLEN key: Returns the length of a list.BRPOP key1 [key2] timeout: Removes and retrieves the last element of a list. If the list is empty, the command blocks until a timeout or an element is available (commonly used for task queues).
Set Operations
Redis sets are unordered collections of string-type elements, where each element is unique (unordered and non-repeating).
Common commands:
SADD key member1 [member2]: Adds one or more members to a set.SMEMBERS key: Retrieves all members of a set.SCARD key: Returns the number of members in a set.SINTER key1 [key2]: Returns the intersection of two or more sets.SUNION key1 [key2]: Returns the union of two or more sets.SDIFF key1 [key2]: Returns the difference between sets (elements unique to the first set).SREM key member1 [member2]: Removes one or more members from a set.
Sorted Set Operations
Redis sorted sets are collections of unique string elements, each associated with a double-precision score for sorting.
Common commands:
ZADD key score1 member1 [score2 member2]: Adds one or more members to a sorted set or updates the score of existing members.ZRANGE key start stop [WITHSCORES]: Retrieves members within a specified index range. Use[WITHSCORES]to include scores.ZINCRBY key increment member: Increments the score of a specified member by a given value.ZREM key member [member ...]: Removes one or more members from a sorted set.
General Key Commands
KEYS pattern: Finds all keys matching a given pattern (e.g.,keys *).EXISTS key: Checks if a key exists.TYPE key: Returns the data type of the value stored at a key.TTL key: Retrieves the time-to-live (TTL) of a key in seconds. A-1indicates the key is set to persist indefinitely.DEL key: Deletes a key if it exists.