Master Redis Data Types and Commands: Strings, Hashes, Lists, Sets, Sorted Sets
This guide provides a comprehensive overview of Redis data structures—including strings, hashes, lists, sets, and sorted sets—along with essential commands for creating, reading, updating, and deleting keys, managing expirations, and performing set operations, enabling developers to effectively leverage Redis as a high‑performance NoSQL database.
Redis Data Types
General Operations
# Check if a key exists
EXISTS name # returns (integer) 1
EXISTS name1 # returns (integer) 0
# Rename a key
RENAME name name2 # OK
# Get the data type of a key
TYPE name2 # string
TYPE PHPREDIS_SESSION:... # string
# Delete a key
DEL name2 # returns (integer) 1
# Get the TTL of a key (in seconds)
TTL age # returns (integer) -1
TTL PHPREDIS_SESSION:... # returns (integer) 1212
# Set expiration (seconds)
EXPIRE aaa 100
# Set expiration (milliseconds)
PEXPIRE aaa 100
# Remove expiration
PERSIST aaaString Operations (strings)
Add
# Set a key
SET name zls # OK
# Set multiple keys
MSET name zls name1 huanglong name2 wuyangke # OK
# Get multiple keys
MGET name name1 name2
1) "zls"
2) "huanglong"
3) "wuyangke"
# Get and set atomically
GETSET xxx 123 # (nil)
GET xxx # "123"
# Set key with expiration (seconds)
SET book hongloumeng EX 100 # OK
# Set key with expiration (milliseconds)
SET book hongloumeng PX 100 # OK
# Increment
INCR zan # returns (integer) 1
INCRBY fans 10000 # returns (integer) 10023
DECR fans # returns (integer) 10012
DECRBY fans 1000 # returns (integer) 9012
INCRBYFLOAT zls 0.1 # returns "1.4"Delete
DEL zls # returns (integer) 1Update
# Append to a string
APPEND name1 liquanyi # returns (integer) 17
GET name1 # "huanglongliquanyi"
# Overwrite part of a string
SETRANGE name1 5 L # returns (integer) 17
GET name1 # "huangLongliquanyi"Query
# Get a single key
GET name1 # "huangLongliquanyi"
# Get multiple keys
MGET name1 name2
1) "huangLongliquanyi"
2) "wuyangke"
# Get string length
STRLEN name1 # returns (integer) 17
STRLEN name2 # returns (integer) 8
# Get TTL (seconds and milliseconds)
TTL name1 # returns (integer) 93
PTTL name1 # returns (integer) 91417
# Get substring
GETRANGE name1 0 4 # "huang"Hash Operations (dictionary type)
Use case: Store partially mutable data such as user profiles or product information; the structure most closely resembles a table.
# Create or update fields
HSET keyname field value
HSET student_id_1 name zls
HSET student_id_1 name zls age 18 gender m
HMSET student_id_1 name zls age 18 gender m
# Retrieve fields
HGET keyname field
HGET student_id_1 name
HGETALL keyname
HGETALL student_id_1
HMGET student_id_1 name age
# Delete fields
HDEL student_id_1 age gender # returns (integer) 2
DEL student_id_1 # delete the whole hash
# Update whole hash
HSET student_id_1 name zls age 18 gender m # returns (integer) 3
HGETALL student_id_1List Operations (list type)
Message queue analogy: A list can act as a simple FIFO queue where producers push items and consumers pop them.
# Push elements to the left (LPUSH)
LPUSH access_log '10.0.0.1 - - [03/Sep/2022:03:38:41 +0800] "POST /index.php HTTP/1.1" 200 1500 "-" "Mozilla/5.0 ..."'
LPUSH access_log '10.0.0.1 - - [03/Sep/2022:03:54:09 +0800] "POST /index.php HTTP/1.1" 200 1500 "-" "Mozilla/5.0 ..."'
# Push to the right (RPUSH)
RPUSH name1 hl
RPUSH name1 wyk
RPUSH name1 zls
# Insert after a specific element
LINSERT name2 AFTER hl hjx # after: insert after; before: insert before
# Get a range of elements
LRANGE name1 0 2
1) "hl"
2) "wyk"
3) "zls"
# Pop from the left (LPOP)
LPOP name1 # "hl"
LPOP name1 # "wyk"
LPOP name1 # "zls"
# Get list length
LLEN name1
# Transfer element from one list to another (RPOPLPUSH)
RPOPLPUSH name1 name2 # moves the rightmost element of name1 to the left of name2
# Remove elements by value (LREM)
LREM name2 7 hl # removes 7 occurrences of "hl"
# Trim list to keep only a range (LTRIM)
LTRIM name2 0 1 # keep first two elements
# Set element by index (LSET)
LSET name1 0 xwq # replace element at index 0
# Get element by index (LINDEX)
LINDEX name1 1 # "hjx"
LINDEX name1 -1 # "hjx" (last element)
# Example: simulate a social feed
LPUSH wechat 'monday hl sb'
LPUSH wechat 'tiusiday hl yiran sb'
LPUSH wechat 'wensiday hl always sb'
LRANGE wechat 0 -1
1) "wensiday hl always sb"
2) "tiusiday hl yiran sb"
3) "monday hl sb"Set Operations (set type)
# Create sets
SADD zls_fans wyk hl
SADD tly_fans mls gaofei wyk
# Membership test
SISMEMBER zls_fans hl # returns (integer) 1
SISMEMBER zls_fans cls # returns (integer) 0
# Get all members
SMEMBERS zls_fans
1) "hl"
2) "wyk"
# Cardinality
SCARD zls_fans # returns (integer) 2
SCARD tly_fans # returns (integer) 3
# Set difference
SDIFF hl_fans tly_fans zls_fans
# Set intersection (common friends)
SINTER hl_fans zls_fans
# Set union
SUNION hl_fans zls_fans
# Store difference in a new set
SDIFFSTORE result tly_fans hl_fans
SCARD result # returns (integer) 2
SMEMBERS result
1) "wyk"
2) "gaofei"
# Random member
SRANDMEMBER zls_fans
# Move element between sets
SMOVE hl_fans zls_fans mls
# Remove element(s)
SPOP hl_fans # removes and returns a random element
SREM zls_fans hl # returns number of removed elements
# Add many numeric members (unordered)
SADD zls_fans 111 222 333 444 555 666Sorted Set Operations (sorted‑set type)
Use case: Leaderboards where members are ordered by a score rather than insertion time.
# Add members with scores
ZADD myzset 1 "one" 2 "two" 3 "three"
# Retrieve members only
ZRANGE chengji 0 -1
1) "wyk"
2) "hl"
3) "zls"
# Retrieve members with scores
ZRANGE chengji 0 -1 WITHSCORES
1) "wyk"
2) "1"
3) "hl"
4) "38"
5) "zls"
6) "100"
# Get rank of a member
ZRANK chengji wyk # 0
ZRANK chengji zls # 2
# Cardinality
ZCARD chengji # 3
# Count members with scores in a range
ZCOUNT chengji 30 40 # 1
ZCOUNT chengji 30 101 # 2
# Get score of a member
ZSCORE chengji wyk # "1"
ZSCORE chengji zls # "100"
# Get members by score range
ZRANGEBYSCORE chengji 30 40
1) "hl"
# Pagination with score range (using LIMIT)
ZRANGEBYSCORE chengji -inf +inf LIMIT 2 3
1) "hjx"
2) "xwq"
3) "zls"
# Delete members by score range
ZREMRANGEBYSCORE chengji 30 40 # returns number removed
# Delete members by rank range
ZREMRANGEBYRANK chengji 0 2 # removes first three members
# Reverse order (high to low)
ZREVRANGE chengji 0 -1 WITHSCORES
1) "zls"
2) "100"
3) "hjx"
4) "60"
5) "xwq"
6) "50"
7) "hl"
8) "38"
9) "wyk"
10) "1"
# Reverse range by score
ZREVRANGEBYSCORE chengji 101 30 WITHSCORES
1) "zls"
2) "100"
3) "hjx"
4) "60"
5) "xwq"
6) "50"
7) "hl"
8) "38"
# Reverse range with LIMIT
ZREVRANGEBYSCORE chengji 101 30 WITHSCORES LIMIT 1 2
1) "hjx"
2) "60"
3) "xwq"
4) "50"Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
