(10-4)redis-hash数据类型
启动redis:
[root@baozi ~]# redis-server /etc/redis.conf
[root@baozi ~]# redis-cli
127.0.0.1:6379>
redis数据类型--hash
1、hash类型的值存储了字段和字段值的映射,字段值只能是字符串,不支持其它数据类型;
2、hash类型的键至多可以存储 2^32 - 1 个字段;
3、redis可以为任何键增减字段而不影响其它键;
login as: root
root@192.168.1.200's password: ******
Last login: Fri May 8 20:00:51 2015 from 192.168.1.1
[root@baozi ~]# redis-server /etc/redis.conf
[root@baozi ~]# redis-cli
//设置字段属性值
127.0.0.1:6379> hset user:100 name zs
(integer) 1
127.0.0.1:6379> hset user:100 age 20
(integer) 1
127.0.0.1:6379> hget user:100 name
"zs"
127.0.0.1:6379> hget user:100 age
"20"
//设置多值
127.0.0.1:6379> hmset user:101 name ls age 19 bir 1992-05-28
OK
127.0.0.1:6379> hmget user:100 name age
1) "zs"
2) "20"
127.0.0.1:6379>
//获得所有值
127.0.0.1:6379> hgetall user:101
1) "name"
2) "ls"
3) "age"
4) "19"
5) "bir"
6) "1992-05-28"
//获得字段
127.0.0.1:6379> hkeys user:101
1) "name"
2) "age"
3) "bir"
//获得字段值
127.0.0.1:6379> hvals user:101
1) "ls"
2) "19"
3) "1992-05-28"
127.0.0.1:6379>
//判断hash键是否存在
127.0.0.1:6379> exists user:101
(integer) 1
//判断字段是否存在
127.0.0.1:6379> hexists user:100 bir
(integer) 0
127.0.0.1:6379> hexists user:101 bir
(integer) 1
//自定义增加字段值
127.0.0.1:6379> hget user:100 age
"20"
127.0.0.1:6379> hincrby user:100 age 2
(integer) 22
//删除某个字段
127.0.0.1:6379> hdel user:101 bir
(integer) 1
//获取某个键所有字段数
127.0.0.1:6379> hlen user:101
(integer) 2
127.0.0.1:6379>
//退出redis
127.0.0.1:6379> shutdown
not connected> quit
[root@baozi ~]#