数据类型应用场景
set key value
get key
1) set user:1 value(json 数据格式)
2)mset user:1:name zhangsan user:1:grade 100
mget user:1:name user:1:grade
id | name | grade |
1 | zhangsan | 100 |
setnx product:1001 value //返回1表示取锁成功,返回0表示取锁失败
//执行业务操作
del product:1001 //释放锁
set prduct:1001 true ex 10 nx //防止程序意外终止导致死锁
incr article:readcount:文章id
get article:readcount:文章id
spring session + redis 实现session共享
incrby orderid 1000 //redis批量生成序列号提高性能
hash常用操作
hset key field value //存储一个哈希表key的键值
hsetnx key field value //存储一个不存在的哈希表key的键值
hmset key field value [field value ...] //一个哈希表key中存储多个键值对
hget key field //获取哈希表key对应的field键值
hmget key field [field ...] //批量获取哈希表key中多个field键值
hdel key field [field ...] //删除哈希表key中field键值
hlen key //返回哈希表key中field的数量
hgetall key //返回哈希表key中所有键值
hincrby key field increment //为哈希表key中field键的值加上增量increment
对象缓存:
hmset user {userId}:name zhangsan {userId}:grade 100
hmset user:1:name zhangsan user:1:grade 100
hmget user:1:name user:1:grade
原文:https://www.cnblogs.com/hongyedeboke/p/12663726.html