Hash
Redis hashes are record types structured as collections of field-value pairs. You can use hashes to represent basic objects and to store groupings of counters, among other things.
HSET
sets the value of one or more fields on a hash.HGET
returns the value at a given field.HMGET
returns the values at one or more given fields.HINCRBY
increments the value at a given field by the integer provided.
HSET "product:501" title "Laptop" price "799" description "Latest model..." stock "150"
(Retrieve title and price for the product)
HMGET "product:501" title price stock
(Decrement stock by 1 when a product is purchased)
HINCRBY "product:501" stock -1
HMGET "product:501" title price stock
HGETALL "product:501"
HDEL "product:501" "stock"
Use Case: User Session Store
userid
1
name
Rachel
ip
10.20.133.233
hits
1
HSET usersession:1 userid 1 name Rachel ip 10.20.133.233 hits 1
# One Value
HGET usersession:1 hits
# Multiple Values
HMGET usersession:1 userid name ip hits
# Increment
HINCRBY usersession:1 hits 1
HDEL usersession:1 hits
# What happens when you GET the deleted key?
HMGET usersession:1 userid name ip hits
EXPIRE
Sets a Key's time to live (TTL). The key will be automatically deleted from Redis once a specific duration (in seconds) has elapsed.
EXPIRE usersession:1 10
DEL
Immediately deletes a key and its associated value from Redis
DEL usersession:1
Last updated