redis是key-value存储的,放在内存中,并在磁盘持久化的数据结构存储系统,它可以用作数据库、缓存和消息中间件。
通过set key value来存储,通过get key来获取值
一.创建key----set/setnx/mset/msetnx
1.创建单个key
set key value:设置key的值,若存在则覆盖
setnx key value:设置key的值,若存在则不操作
192.168.10.12:6379> set gw1 10
OK
192.168.10.12:6379> get gw1
"10"
192.168.10.12:6379> keys *
1) "gw1"
192.168.10.12:6379> set gw2 20
OK
192.168.10.12:6379> get gw2
"20"
192.168.10.12:6379> keys *
1) "gw1"
2) "gw2"
192.168.10.12:6379> setnx gw4 40
(integer) 1
192.168.10.12:6379> get gw4
"40"
192.168.10.12:6379> setnx gw4 50
(integer) 0
192.168.10.12:6379> get gw4
"40"
2.创建多个key
MSET key1 value1 key2 value2 ... keyN valueN :设置这些key的值,若存在则覆盖
MSETNX key1 value1 key2 value2 ... keyN valueN:同mset,但如果其中一个key已经存在了,则都不设置。这些操作都是原子的
192.168.10.12:6379> mset g1 a g2 b g3 c
OK
192.168.10.12:6379> keys *
4) "g2"
7) "g1"
8) "g3"
192.168.10.12:6379> mset g3 d g4 f g5 g
OK
192.168.10.12:6379> keys *
5) "g4"
9) "g3"
11) "g5"
192.168.10.12:6379> get g3
"d"
192.168.10.12:6379> get g4
"f"
192.168.10.12:6379> get g5
"g"
192.168.10.12:6379> msetnx g5 h g6 i g7k
(integer) 0
192.168.10.12:6379> keys *
5) "g4"
9) "g3"
11) "g5"
192.168.10.12:6379> get g5
"g"
192.168.10.12:6379> get g6
(nil)
二.查看key
1.查看所有key----keys *
keys *
keys "*"
2.根据key查看value----getkey
192.168.10.12:6379> set gw1 10
OK
192.168.10.12:6379> get gw1
"10"
192.168.10.12:6379> keys *
1) "gw1"
192.168.10.12:6379> set gw2 20
OK
192.168.10.12:6379> get gw2
"20"
192.168.10.12:6379> keys *
1) "gw1"
2) "gw2"
3.查看匹配前缀或后缀的key----keys *substr*
192.168.10.12:6379> keys *
1) "gw1"
2) "gw2"
3) "gao2"
4) "gao1"
192.168.10.12:6379> keys gw*
1) "gw1"
2) "gw2"
192.168.10.12:6379> keys gao*
1) "gao2"
2) "gao1"
192.168.10.12:6379> keys *g*
1) "gw1"
2) "gw2"
3) "gao2"
4) "gao1"
192.168.10.12:6379> keys *1
1) "gw1"
2) "gao1"
4.随机取出一个key----randomkey
192.168.10.12:6379> randomkey
"gao2"
192.168.10.12:6379> randomkey
"gao2"
192.168.10.12:6379> randomkey
"gw2"
192.168.10.12:6379> randomkey
"gw2"
192.168.10.12:6379> randomkey
"gao2"
5.查看key的类型----typekey key
192.168.10.12:6379> type gw1
string
192.168.10.12:6379> type gao1
string
6.查看当前数据库中key的数量----dbsize
192.168.10.12:6379> keys *
1) "gw1"
2) "gw2"
3) "gao2"
4) "gao1"
192.168.10.12:6379> dbsize
(integer) 4
192.168.10.12:6379> select 1
OK
192.168.10.12:6379[1]> dbsize
(integer) 0
192.168.10.12:6379[1]> select 0
OK
192.168.10.12:6379> dbsize
(integer) 4
7.查看某一个key是否存在----exists key
192.168.10.12:6379> exists gao1
(integer) 1
192.168.10.12:6379> exists ggao1
(integer) 0
三.删除key----del key
192.168.10.12:6379> del gao1
(integer) 1
192.168.10.12:6379> get gao1
(nil)
192.168.10.12:6379> del gao1
(integer) 0
四.重命名key----rename old new
192.168.10.12:6379> get gw1
"10"
192.168.10.12:6379> rename gw1 ggw1
OK
192.168.10.12:6379> get gw1
(nil)
192.168.10.12:6379> get ggw1
"10"
五.向key的字符串追加拼接----append key value
192.168.10.12:6379> get gao1
"a"
192.168.10.12:6379> append gao1 b
(integer) 2
192.168.10.12:6379> get gao1
"ab"
六.获取并重设某个key----getset key value
获取该元素的值,并给该元素设置新值。
通常和incr搭配使用,比如一个mycount一直incr,然后达到某些情况需要清零,清零之前需要知道mycount的值
192.168.10.12:6379> get gao1
"ab"
192.168.10.12:6379> getset gao1 cd
"ab"
192.168.10.12:6379> get gao1
"cd"