key定义的注意点:
1)key不能过长,也不能太短
2)key需要有统一的命名规范
3)在同一个名称空间(database),key必须要保持唯一
* 字符串(string)
查看帮助: help @string 或者 help COMMAND
value最多可以容纳的数据长度是512M
常用命令:
~~~
192.168.20.144:6379> set company als nx (nx表示,当key(company)不存在,者就赋值,如果存在,就报nil)
OK
192.168.20.144:6379> get company (get命令:获取key的值)
"als"
192.168.20.144:6379> append company lsf (append命令: 追加)
(integer) 6
192.168.20.144:6379> set person jack
OK
192.168.20.144:6379> get person
"jack"
192.168.20.144:6379> del person (del命令:删除一个key)
(integer) 1
192.168.20.144:6379> get person
(nil)
192.168.20.144:6379> incr studyID (incr命令:至增,如果原先没有这个值,就默认是0,然后默认至增为1,所以显示为1)
(integer) 1
192.168.20.144:6379> incr studyID
(integer) 2
192.168.20.144:6379> incr studyID
(integer) 3
192.168.20.144:6379> incr studyID
(integer) 4
(incrby命令: 指定一次增长多少)
192.168.20.144:6379> incrby studyID 2
(integer) 6
192.168.20.144:6379> incrby studyID 2
(integer) 8
(decr命令:每次依次减少1)
192.168.20.144:6379> decr studyID
(integer) 7
192.168.20.144:6379> decr studyID
(integer) 6
(decrby命令: 指定每次减少几个)
192.168.20.144:6379> decrby studyID 2
(integer) 4
~~~
* 存储Hash
~~~
192.168.20.144:6379> hset myhash username jack
(integer) 1
192.168.20.144:6379> hset myhash age 30
(integer) 1
(hset命令:赋值)
192.168.20.144:6379> hmset myhash02 username louis age 20
OK
(hmset命令:一次性设置多个key的值)
192.168.20.144:6379> hget myhash username
"jack"
192.168.20.144:6379> hget myhash02 username
"louis"
(hget命令:获取指定key的值)
192.168.20.144:6379> hmget myhash username age
1) "jack"
2) "30"
(hmget命令:一次性获取多个key的值)
192.168.20.144:6379> hgetall myhash
1) "username"
2) "jack"
3) "age"
4) "30"
(hgetall命令:一次性获取这个hash的所有key和value)
192.168.20.144:6379> hdel myhash02 username age
(integer) 2
192.168.20.144:6379> hgetall myhash02
(empty list or set)
(hdel命令:删除一个hash中的某个key)
192.168.20.144:6379> hmset myhash02 username louis age 20
OK
192.168.20.144:6379> hget myhash02 age
"20"
192.168.20.144:6379> HINCRBY myhash02 age 5
(integer) 25
(HEXISTS命令:用来判断指定的key(属性)是否存在,如果存在,就显示1,如果不存在者显示0)
192.168.20.144:6379> HEXISTS myhash username
(integer) 1
192.168.20.144:6379> HEXISTS myhash ages
(integer) 0
(HKEYS命令: 列出指定hash的所有的key)
192.168.20.144:6379> HKEYS myhash
1) "username"
2) "age"
(HVALS命令:列出指定hash的所有value)
192.168.20.144:6379> HVALS myhash
1) "jack"
2) "35"
~~~
* 存储list
使用数组方式(ArrayList)
使用双向链接方式(LinkedList)
双向链表添加删除数据
~~~
(lpush和rpush命令:两端添加)
192.168.20.144:6379> lpush mylist a b
(integer) 2
192.168.20.144:6379> lpush mylist 1
(integer) 3
192.168.20.144:6379> rpush mylist aa
(integer) 4
(Lrange:查看列表的值,start为0,stop:5;-1表示最后一个)
192.168.20.144:6379> LRANGE mylist 0 5
1) "1"
2) "b"
3) "a"
4) "aa"
192.168.20.144:6379> LRANGE mylist 0 -2
1) "1"
2) "b"
3) "a"
(LPOP和RPOP命令:两端弹出)
192.168.20.144:6379> LPOP mylist
"1"
192.168.20.144:6379> LRANGE mylist 0 5
1) "b"
2) "a"
3) "aa"
192.168.20.144:6379> RPOP mylist
"aa"
192.168.20.144:6379> LRANGE mylist 0 5
1) "b"
2) "a"
(LPUSHX命令:仅当mylist存在,才会被插入相应的值,如果不存在,者显示0)
192.168.20.144:6379> LLEN mylist
(integer) 2
192.168.20.144:6379> LPUSHX mylist x
(integer) 3
192.168.20.144:6379> LRANGE mylist 0 -1
1) "x"
2) "b"
3) "a"
192.168.20.144:6379> LPUSHX mylist1 x
(integer) 0
192.168.20.144:6379> LSET mylist 1 10
OK
192.168.20.144:6379> LRANGE mylist 0 -1
1) "1"
2) "10"
3) "x"
4) "b"
5) "a"
(linsert命令:在指定位置插入值)
192.168.20.144:6379> LPUSH mylist01 a b c
(integer) 3
192.168.20.144:6379> LRANGE mylist01 0 -1
1) "c"
2) "b"
3) "a"
192.168.20.144:6379> LINSERT mylist01 before b 11
(integer) 4
192.168.20.144:6379> lrange mylist01 0 -1
1) "c"
2) "11"
3) "b"
4) "a"
~~~
* 存储set
和list类型不同的是,set集合中不允许出现重复的元素
~~~
192.168.20.144:6379> sadd myset01 a b c (向集合中添加成员)
(integer) 3
192.168.20.144:6379> SREM myset01 a (删除集合中的成员)
(integer) 1
192.168.20.144:6379> SMEMBERS myset01 (显示集合中的成员)
1) "b"
2) "c"
192.168.20.144:6379> SISMEMBER myset a (判断集合中是否存在指定的)
(integer) 1
192.168.20.144:6379> sadd mya1 a b c d
(integer) 4
192.168.20.144:6379> sadd myb1 a c 1 2
(integer) 4
192.168.20.144:6379> SDIFF mya1 myb1 (差级运算,这个跟key的顺序有关)
1) "b"
2) "d"
192.168.20.144:6379> SDIFF myb1 mya1
1) "1"
2) "2"
192.168.20.144:6379> SINTER mya1 myb1 (交集运算)
1) "a"
2) "c"
192.168.20.144:6379> SUNION mya1 myb1 (并集运算,重复的只会出现一次)
1) "d"
2) "b"
3) "c"
4) "1"
5) "a"
6) "2"
~~~
* 存储sorted-set
~~~
192.168.20.144:6379> zadd mysort01 100 a
(integer) 1
192.168.20.144:6379> zadd mysort01 100 b
(integer) 1
192.168.20.144:6379> zadd mysort01 99 c
(integer) 1
192.168.20.144:6379> ZRANGE mysort01
(error) ERR wrong number of arguments for 'zrange' command
192.168.20.144:6379> ZRANGE mysort01 0 -1
1) "c"
2) "a"
3) "b"
192.168.20.144:6379> ZREM mysort01 c
(integer) 1
192.168.20.144:6379> ZRANGE mysort01 0 -1
1) "a"
2) "b"
192.168.20.144:6379> ZRANK mysort01
(error) ERR wrong number of arguments for 'zrank' command
192.168.20.144:6379> ZRANK mysort01 c
(nil)
192.168.20.144:6379> ZRANK mysort01 b
(integer) 1
192.168.20.144:6379> ZRANK mysort01 a
(integer) 0
~~~