Skip to content

Instantly share code, notes, and snippets.

@minhphong306
Forked from LeCoupa/redis_cheatsheet.bash
Last active February 13, 2021 03:29
Show Gist options
  • Save minhphong306/a00b90cd749e5454df58c42a29213022 to your computer and use it in GitHub Desktop.
Save minhphong306/a00b90cd749e5454df58c42a29213022 to your computer and use it in GitHub Desktop.
Redis Cheatsheet phiên bản tiếng Việt
# Redis Cheatsheet
# Bản này mình fork về và dịch sang tiếng Việt cho dễ đọc
redis-server /path/redis.conf # khởi động redis-server từ file config
redis-cli # Mở redis-cli
# Strings.
APPEND key value # Append thêm value vào key (vd đang là "aaa", gõ "append bbb" => "aaabbb")
BITCOUNT key [start end] # Cái này chưa nắm được
SET key value # Set value
SETNX key value # set nếu key chưa tồn tại
SETRANGE key offset value # Ghi đè giá trị của key từ vị trí offset
STRLEN key # Lấy độ dài value của 1 key cụ thể
MSET key value [key value ...] # Set nhiều value
MSETNX key value [key value ...] # Set nhiều value nếu key chưa tồn tại
GET key # Get value của key ra
GETRANGE key start end # Get 1 substring của key ra,từ vị trí start đến vị trí end
MGET key [key ...] # Get value của nhiều key
INCR key # Tăng giá trị lên 1 (dùng cho value là số)
INCRBY key increment # Tăng giá trị lên 1 lượng increment
INCRBYFLOAT key increment # Vẫn là tăng giá trị lên 1 lượng increment nhưng là float
DECR key # Giảm giá trị đi 1
DECRBY key decrement # Giảm giá trị đi 1 lượng decrement
DEL key # Xóa key
EXPIRE key 120 # Đặt thời gian hết hạn cho key, như vd là sau 120s hết hạn
TTL key # Xem thời gian hết hạn của key còn bao nhiêu giây. -1 là ko hết hạn
# Lists.
# Danh sách các phần tử có thứ tự
RPUSH key value [value ...] # Thêm phần tử vào cuối
RPUSHX key value # Thêm phần tử vào cuối nếu chưa tồn tại
LPUSH key value [value ...] # Thêm phần tử vào đầu
LRANGE key start stop # Lấy danh sách phần tử từ vị trí start đến stop
LINDEX key index # Lấy phần tử ở vị trí index (tính từ 0)
LINSERT key BEFORE|AFTER pivot value # Thêm phần tử vào trước/sau vị trí pivot
LLEN key # Lấy độ dài của list
LPOP key # Lấy ra phần tử đầu tiên của list
LSET key index value # Set giá trị cho phần tử ở vị trí index
LTRIM key start stop # Cắt list từ vị trí start đến stop (vd: ltrim list1 2 5 => cắt list1 chỉ còn các phần tử từ 2 đến 5)
RPOP key # Lấy ra phần tử cuối cùng của list
RPOPLPUSH source destination # Lấy phần tử từ cuối 1 list, đưa vào đầu 1 list khác
BLPOP key [key ...] timeout # Lấy ra phần tử đầu tiên, nếu ko có thì đợi cho tới khi timeout
BRPOP key [key ...] timeout # Lấy ra phần tử cuối cùng, nếu ko có thì đợi tới khi timeout
# Sets.
# Gần giống list, nhưng không có thứ tự, và mỗi phần tử chỉ xuất hiện 1 lần
SADD key member [member ...] # Thêm phần tử vào set
SCARD key # Đếm số phần tử của set
SREM key member [member ...] # Remove phần tử khỏi set
SISMEMBER myset value # Kiểm tra phần tử có nằm trong set không
SMEMBERS myset # Lấy danh sách phần tử trong set
SUNION key [key ...] # Gộp 2 set lại và trả kết quả về
SINTER key [key ...] # Tìm tập giao của 2 set
SMOVE source destination member # Chuyển 1 phần tử từ setA sang setB
SPOP key [count] # Lấy ra <count> phần tử ngẫu nhiên trong set
# Sorted Sets
# Giống set, nhưng mỗi phần tử có điểm trọng số. Cháu nào điểm cao hơn thì nằm trên
ZADD key [NX|XX] [CH] [INCR] score member [score member ...] # Thêm phần tử vào set.
# Đoạn này hơi nhiều nên làm example cho rõ ràng
# NX: Chỉ update exist element, không add element mới
# XX: Chỉ add element mới, không update element
# CH
ZCARD key # Đếm số phần tử của sorted set
ZCOUNT key min max # Đếm số phần tử có score nằm trong khoảng từ <min> đến <max>
ZINCRBY key increment member # Tăng giá trị của phần tử trong sorted set 1 lượng <increment>
ZRANGE key start stop [WITHSCORES] # Trả về sorted set con từ vị trí <start> đến vị trí <end>
ZRANK key member # Lấy ra vị trí của phần tử trong sorted set
ZREM key member [member ...] # Xóa phần tử khỏi sorted set
ZREMRANGEBYRANK key start stop # Xóa các phần tử từ vị trí <start> đến <stop>
ZREMRANGEBYSCORE key min max # Xóa phần tử có giá trị trong khoảng từ <min> đến <max>
ZSCORE key member # Lấy ra score của phần tử trong set
ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count] # return a range of members in a sorted set, by score
# Hashes
# Hashes are maps between string fields and string values, so they are the perfect data type to represent objects.
HGET key field # get the value of a hash field
HGETALL key # get all the fields and values in a hash
HSET key field value # set the string value of a hash field
HSETNX key field value # set the string value of a hash field, only if the field does not exists
HMSET key field value [field value ...] # set multiple fields at once
HINCRBY key field increment # increment value in hash by X
HDEL key field [field ...] # delete one or more hash fields
HEXISTS key field # determine if a hash field exists
HKEYS key # get all the fields in a hash
HLEN key # get all the fields in a hash
HSTRLEN key field # get the length of the value of a hash field
HVALS key # get all the values in a hash
# HyperLogLog
# HyperLogLog uses randomization in order to provide an approximation of the number of unique elements in a set using just a constant, and small, amount of memory
PFADD key element [element ...] # add the specified elements to the specified HyperLogLog
PFCOUNT key [key ...] # return the approximated cardinality of the set(s) observed by the HyperLogLog at key's)
PFMERGE destkey sourcekey [sourcekey ...] # merge N HyperLogLogs into a single one
# Publication & Subscription
PSUBSCRIBE pattern [pattern ...] # listen for messages published to channels matching the given patterns
PUBSUB subcommand [argument [argument ...]]  # inspect the state of the Pub/Sub subsystem
PUBLISH channel message # post a message to a channel
PUNSUBSCRIBE [pattern [pattern ...]]  # stop listening for messages posted to channels matching the given patterns
SUBSCRIBE channel [channel ...]  # listen for messages published to the given channels
UNSUBSCRIBE [channel [channel ...]] # stop listening for messages posted to the given channels
# Other Commands
KEYS pattern # find all keys matching the given pattern
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment