Redis教程

Redis的Hash操作

本文主要是介绍Redis的Hash操作,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

常用命令

127.0.0.1:6379> hset user:1001 id 1     												 ##存值
(integer) 1
127.0.0.1:6379> hset user:1001 name zhangsan                        ##存值
(integer) 1
127.0.0.1:6379> hset user:1001 age 22	                                       ##存值
(integer) 1
127.0.0.1:6379> hget user:1001 id                                             ##取值
"1"
127.0.0.1:6379> hget user:1001 name                                   ##取值
"zhangsan"
127.0.0.1:6379> hget user:1001 age                                  ##取值
"22"
127.0.0.1:6379> 
------------------------------------
127.0.0.1:6379> hsetnx user:1003 x1 v1##当某个field不存在才允许添加
(integer) 1
127.0.0.1:6379> hsetnx user:1003 age 33
(integer) 0
127.0.0.1:6379> 

-------------------------------------
127.0.0.1:6379> hmset user:1003 id 2 name lisi age 23##批量插入
OK
127.0.0.1:6379> hget user:1003 id
"2"
127.0.0.1:6379> hget user:1003 name
"lisi"
127.0.0.1:6379> hget user:1003 age
"23"
127.0.0.1:6379>
-------------------------------------------------------
127.0.0.1:6379> hexists user:1003 gender##返回某个field是否存在
(integer) 0
127.0.0.1:6379> hexists user:1003 age
(integer) 1
127.0.0.1:6379> 
--------------------------------------------------
127.0.0.1:6379> hkeys user:1003##查看某个key的所有field
1) "id"
2) "name"
3) "age"
127.0.0.1:6379> 
-----------------------------------------------
127.0.0.1:6379> hvals user:1003##查看某个key的所有field的值
1) "2"
2) "lisi"
3) "23"
127.0.0.1:6379> 
----------------------------------------------
127.0.0.1:6379> hincrby user:1003 age 5##增加某个field的值
(integer) 28
127.0.0.1:6379> hget user:1003 age
"28"
127.0.0.1:6379> 
-------------------------------------------------



这篇关于Redis的Hash操作的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!