Redis-数据类型和基础命令
数据类型:
Key:
1、查看所有key
localhost:6379> keys *
1) "k1"
2、设置key,获取key值,删除key
localhost:6379> set k1 v1 #设置单个key
OK
localhost:6379> mset K1 V1 K2 V2 K3 V3 #设置多个key
OK
localhost:6379> get k1 #获取单个key
"v1"
localhost:6379> mget K1 K2 K3 #获取多个key
1) "V1"
2) "V2"
3) "V3"
localhost:6379> del k1 #删除key
(integer) 1
localhost:6379> get k1
(nil)
3、查看key类型
localhost:6379> keys *
1) "k1"
localhost:6379> type k1
string
4、判断key是否存在
localhost:6379> keys *
1) "k1"
localhost:6379> exists k1
(integer) 1
localhost:6379> exists k2
(integer) 0
5、移除当前key
localhost:6379> move k1 1 #move key db编号
(integer) 0
6、设置和查看 key 的过期时间,单位是秒(-1 永不过期;-2 没有这个key)
localhost:6379> EXPIRE k2 10 #设置过期时间
(integer) 1
localhost:6379> ttl k2 #查看过期时间
(integer) 8
localhost:6379> ttl k2
(integer) 7
localhost:6379> ttl k2
(integer) 4
localhost:6379> ttl k2
(integer) 1
localhost:6379> ttl k2
(integer) -2
7、取消过期时间
localhost:6379> set k2 v2
OK
localhost:6379> expire k2 10 #设置过期时间
(integer) 1
localhost:6379> ttl k2 #查看过期时间
(integer) 8
localhost:6379> ttl k2
(integer) 6
localhost:6379> persist k2 #取消过期时间
(integer) 1
localhost:6379> ttl k2
(integer) -1 #-1 永不过期
8、计算器
localhost:6379> set k3 1
OK
localhost:6379> incr k3 #每次执行一次就+1
(integer) 2
localhost:6379> incr k3
(integer) 3
localhost:6379> incr k3
(integer) 4
localhost:6379> incrby k3 100 #设置key加多少
(integer) 104
String:
localhost:6379> set key1 v1
OK
localhost:6379> get key1
"v1"
localhost:6379> append key1 "hello" #追加字符串
(integer) 7
localhost:6379> get key1
"v1hello"
localhost:6379> strlen key1 #查看key值长度
(integer) 7
localhost:6379> append key1 ",world!"
(integer) 14
localhost:6379> strlen key1
(integer) 14
List:
#LPUSH可向list的左边(头部)添加一个新元素
#RPUSH可向list的右边(头部)添加一个新元素
#LRANGE可从list中取出一定范围的元素
localhost:6379> lpush list1 A
(integer) 1
localhost:6379> lpush list1 B
(integer) 2
localhost:6379> rpush list1 0
(integer) 3
localhost:6379> rpush list1 1
(integer) 4
localhost:6379> llen list1
(integer) 4
localhost:6379> lrange list1 0 -1 #输入不存在的从而到达查看list中全部的值
1) "B"
2) "A"
3) "0"
4) "1"
localhost:6379> lrange list1 1 1 #查看1-1这个范围
1) "A"
localhost:6379> lrange list1 1 3 #查看1-3这个范围
1) "A"
2) "0"
3) "1"
localhost:6379> rpop list1 #从右边开始删除
"1"
localhost:6379> lrange list1 1 3
1) "A"
2) "0"
localhost:6379> lpop list1 #从左边开始删除
"B"
localhost:6379> lrange list1 0 -1
1) "A"
2) "0"
Hash:
localhost:6379> hmset user:1000 name cai age 20 job it
OK
localhost:6379> hmget user:1000 name #查看这个哈希某个字段
1) "cai"
localhost:6379> hmget user:1000 age
1) "20"
localhost:6379> hmget user:1000 job
1) "it"
localhost:6379> hgetall user:1000 #查看这个哈希全部字段
1) "name"
2) "cai"
3) "age"
4) "20"
5) "job"
6) "it"
Set:
localhost:6379> sadd set1 1 2 3 4 5 #创建集合
(integer) 5
localhost:6379> sadd set2 3 6 2 10
(integer) 4
localhost:6379> SMEMBERS set1 #查看集合
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
localhost:6379> SMEMBERS set2
1) "2"
2) "3"
3) "6"
4) "10"
localhost:6379> sadd set2 3 6 2 10 11 3 5 6
(integer) 2
localhost:6379> SMEMBERS set2 #不允许出现重复
1) "2"
2) "3"
3) "5"
4) "6"
5) "10"
6) "11"
localhost:6379> sdiff set1 set2 #以第一个为基准,找二个没有的
1) "1"
2) "4"
localhost:6379> SMEMBERS set1
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
localhost:6379> SMEMBERS set2
1) "2"
2) "3"
3) "5"
4) "6"
5) "10"
6) "11"
localhost:6379> sdiff set2 set1 #以第二个为基准,找第一个没有的
1) "6"
2) "10"
3) "11"
localhost:6379> sinter set1 set2 #以第一个为准,求共有的
1) "2"
2) "3"
3) "5"
localhost:6379> sunion set1 set2 #去重后,按顺序排
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "10"
8) "11"