目录
SETNX添加String类型键值对命令(前提是key不存在,否则不执行)
问题:Redis没有类似Mysql的Table的概念,我们该如何区分不同类型的key呢?
HSET key field value:添加或者修改hash类型的key和fired的值
HGET key field:获取一个hash类型的key的field的值
HGETALL:获取一个hash类型的key中所有的field和value
HKEYS:获取一个hash类型的key中的所有的field
HVALS:获取一个hash类型的key中的所有的value
HINCRBY:让一个hash类型key的字段值自增并指定步长
HSETNX:添加一个hash类型的key的field值,前提是这个field不存在,否则不执行
参考视频:黑马程序员Redis入门到实战教程,深度透析redis底层原理+redis分布式锁+企业解决方案+黑马点评实战项目
我们能通过官方网站提供的文档来了解我们需要使用的redis数据类型
或者是使用命令help来查看
192.168.1.209:6379> help @generic
我们能通过help [command]可以查看一个命令的具体用法
- 192.168.1.209:6379> help KEYS
-
- KEYS pattern
- summary: Find all keys matching the given pattern
- since: 1.0.0
- group: generic
-
- 192.168.1.209:6379>
使用KEYS命令查看所有的key
- 192.168.1.209:6379> SET name "claylpf"
- OK
- 192.168.1.209:6379> SET id 66
- OK
- 192.168.1.209:6379> KEYS *
- 1) "id"
- 2) "name"
- 192.168.1.209:6379>
例如:
具体用法
- 192.168.1.209:6379> help DEL
-
- DEL key [key ...]
- summary: Delete a key
- since: 1.0.0
- group: generic
-
- 192.168.1.209:6379>
例如:
- 192.168.1.209:6379> DEL id
- (integer) 1
- 192.168.1.209:6379> KEYS *
- 1) "name"
- 192.168.1.209:6379>
多重删除
- 192.168.1.209:6379> DEL k1 k2 k3
- (integer) 3
- 192.168.1.209:6379> KEYS *
- 1) "age"
- 2) "name"
- 192.168.1.209:6379>
具体用法:
- 192.168.1.209:6379> help EXISTS
-
- EXISTS key [key ...]
- summary: Determine if a key exists
- since: 1.0.0
- group: generic
-
- 192.168.1.209:6379>
如:
- 192.168.1.209:6379> KEYS *
- 1) "age"
- 2) "name"
- 192.168.1.209:6379> EXISTS age
- (integer) 1
- 192.168.1.209:6379> EXISTS id
- (integer) 0
- 192.168.1.209:6379>
具体用法:
- 192.168.1.209:6379> help EXPIRE
-
- EXPIRE key seconds
- summary: Set a key's time to live in seconds
- since: 1.0.0
- group: generic
-
- 192.168.1.209:6379>
例如:
- 192.168.1.209:6379> EXPIRE age 20
- (integer) 1
- 192.168.1.209:6379> TTL age
- (integer) 13
- 192.168.1.209:6379>
具体用法:
- 192.168.1.209:6379> help TTL
-
- TTL key
- summary: Get the time to live for a key
- since: 1.0.0
- group: generic
-
- 192.168.1.209:6379>
例如:
当我们使用TTL查看到剩余时间为-1的时候,他表示的就是永久有效
- 192.168.1.209:6379> TTL age
- (integer) 13
- 192.168.1.209:6379> TTL name
- (integer) -1
- 192.168.1.209:6379> KEYS * # 因为age的有效时间到了,所以age消失了,而只剩下name的永久有效
- 1) "name"
- 192.168.1.209:6379>
其中value是字符串,不过根据字符串的格式不同,可以分为3类:
注:不管是那种格式,底层都是字节数组形式存储的,只不过是编码的方式不同。字符串类型的最大空间不能超过512mb
我们能通过help [command]可以查看一个命令的具体用法
具体用法
- 192.168.1.209:6379> help SET
-
- SET key value [EX seconds|PX milliseconds|EXAT timestamp|PXAT milliseconds-timestamp|KEEPTTL] [NX|XX] [GET]
- summary: Set the string value of a key
- since: 1.0.0
- group: string
-
- 192.168.1.209:6379>
插入数据
- 192.168.1.209:6379> SET age 20
- OK
- 192.168.1.209:6379> KEYS a*
- 1) "age"
- 192.168.1.209:6379>
多重插入
- 192.168.1.209:6379> MSET k1 v1 k2 v2 k3 v3
- OK
- 192.168.1.209:6379> KEYS *
- 1) "age"
- 2) "k1"
- 3) "k3"
- 4) "name"
- 5) "k2"
- 192.168.1.209:6379>
具体用法:
- 192.168.1.209:6379> help GET
-
- GET key
- summary: Get the value of a key
- since: 1.0.0
- group: string
-
- 192.168.1.209:6379>
例如:
- 192.168.1.209:6379> GET name
- "claylpf"
- 192.168.1.209:6379>
- 192.168.1.209:6379> MSET k1 v1 k2 v2 k3 v3
- OK
- 192.168.1.209:6379> KEYS *
- 1) "k1"
- 2) "k3"
- 3) "name"
- 4) "k2"
- 192.168.1.209:6379> MGET k1 k2 k3
- 1) "v1"
- 2) "v2"
- 3) "v3"
- 192.168.1.209:6379>
- 192.168.1.209:6379> GET age
- "18"
- 192.168.1.209:6379> INCR age
- (integer) 19
- 192.168.1.209:6379> INCR age
- (integer) 20
- 192.168.1.209:6379>
- 192.168.1.209:6379> GET age
- "20"
- 192.168.1.209:6379> INCRBY age 2
- (integer) 22
- 192.168.1.209:6379> INCRBY age 2
- (integer) 24
- 192.168.1.209:6379> INCRBY age 3
- (integer) 27
- 192.168.1.209:6379>
也可以实现自减操作
- 192.168.1.209:6379> INCRBY age 3
- (integer) 27
- 192.168.1.209:6379> INCRBY age -1
- (integer) 26
- 192.168.1.209:6379> INCRBY age -1
- (integer) 25
- 192.168.1.209:6379> INCRBY age -2
- (integer) 23
- 192.168.1.209:6379>
- 192.168.1.209:6379> set float1 10.11
- OK
- 192.168.1.209:6379> GET float1
- "10.11"
- 192.168.1.209:6379> INCRBYFLOAT float1 0.5
- "10.61"
- 192.168.1.209:6379> INCRBYFLOAT float1 0.5
- "11.11"
- 192.168.1.209:6379> INCRBYFLOAT float1 -1.5
- "9.61"
- 192.168.1.209:6379> INCRBYFLOAT float1 -1.5
- "8.11"
- 192.168.1.209:6379>
具体用法:
- 192.168.1.209:6379> help SETNX
-
- SETNX key value
- summary: Set the value of a key, only if the key does not exist
- since: 1.0.0
- group: string
-
- 192.168.1.209:6379>
例如:
- 192.168.1.209:6379> SETNX name2 lisi
- (integer) 1
- 192.168.1.209:6379> get name2
- "lisi"
- 192.168.1.209:6379> SETNX name lisi2
- (integer) 0
- 192.168.1.209:6379> get name
- "claylpf"
- 192.168.1.209:6379>
具体用法:
- 192.168.1.209:6379> help SETEX
-
- SETEX key seconds value
- summary: Set the value and expiration of a key
- since: 2.0.0
- group: string
-
- 192.168.1.209:6379>
例如:
- 192.168.1.209:6379> SETEX name3 30 jack
- OK
- 192.168.1.209:6379> TTL name3
- (integer) 24
- 192.168.1.209:6379> TTL name3
- (integer) 21
- 192.168.1.209:6379> TTL name3
- (integer) 19
- 192.168.1.209:6379> GET name3
- "jack"
- 192.168.1.209:6379>
例如:当我们需要存储用户、商品的信息到redis,如果有一个用户的id是1,而有一个商品的id恰好是也是1,我们应该如何解决这个问题呢。
我们可以通过拼接key的结构实现多层级结构
这个格式并非固定的,我们也可以根据自己的需求来删除或者添加词条。
实操:
String结构是将对象序列化为JSON字符串后存储,当需要修改对象某个字段时很不方便
因此,我们需要使用到Hash类型。
Hash结构可以将对象中的每个字段独立存储,可以针对单个字段做CRUD(增删改查)
- 192.168.1.209:6379> HSET heima:user:3 name lucy
- (integer) 1
- 192.168.1.209:6379> HSET heima:user:3 age 22
- (integer) 1
- 192.168.1.209:6379> HSET heima:user:4 name band
- (integer) 1
- 192.168.1.209:6379> HSET heima:user:4 age 23
- (integer) 1
- 192.168.1.209:6379> KEYS heima*
- 1) "heima:user:4"
- 2) "heima:user:3"
- 192.168.1.209:6379> HGET heima:user:3 name
- "lucy"
- 192.168.1.209:6379> HGET heima:user:3 age
- "22"
- 192.168.1.209:6379> HGET heima:user:4 name
- "band"
- 192.168.1.209:6379> HGET heima:user:4 age
- "23"
- 192.168.1.209:6379>
- 192.168.1.209:6379> HMSET heima:user:5 name clay age 24 sex man
- OK
- 192.168.1.209:6379> HMGET heima:user:5 name age sex
- 1) "clay"
- 2) "24"
- 3) "man"
- 192.168.1.209:6379>
- 192.168.1.209:6379> HMSET heima:user:5 name clay age 24 sex man
- OK
- 192.168.1.209:6379> HMGET heima:user:5 name age sex
- 1) "clay"
- 2) "24"
- 3) "man"
- 192.168.1.209:6379>
- 192.168.1.209:6379> HGETALL heima:user:4
- 1) "name"
- 2) "band"
- 3) "age"
- 4) "23"
- 192.168.1.209:6379> HGETALL heima:user:5
- 1) "name"
- 2) "clay"
- 3) "age"
- 4) "24"
- 5) "sex"
- 6) "man"
- 192.168.1.209:6379>
- 192.168.1.209:6379> HKEYS heima:user:5
- 1) "name"
- 2) "age"
- 3) "sex"
- 192.168.1.209:6379> HKEYS heima:user:4
- 1) "name"
- 2) "age"
- 192.168.1.209:6379>
- 192.168.1.209:6379> HVALS heima:user:4
- 1) "band"
- 2) "23"
- 192.168.1.209:6379> HVALS heima:user:5
- 1) "clay"
- 2) "24"
- 3) "man"
- 192.168.1.209:6379>
- 192.168.1.209:6379> HINCRBY heima:user:4 age 2
- (integer) 25
- 192.168.1.209:6379> HINCRBY heima:user:4 age 2
- (integer) 27
- 192.168.1.209:6379> HVALS heima:user:4
- 1) "band"
- 2) "27"
- 192.168.1.209:6379> HINCRBY heima:user:4 age -10
- (integer) 17
- 192.168.1.209:6379> HVALS heima:user:4
- 1) "band"
- 2) "17"
- 192.168.1.209:6379>
- 192.168.1.209:6379> HSETNX heima:user:4 sex woman
- (integer) 1
- 192.168.1.209:6379> HGETALL heima:user:4
- 1) "name"
- 2) "band"
- 3) "age"
- 4) "17"
- 5) "sex"
- 6) "woman"
- 192.168.1.209:6379> HSETNX heima:user:5 sex woman
- (integer) 0
- 192.168.1.209:6379> HGETALL heima:user:5
- 1) "name"
- 2) "clay"
- 3) "age"
- 4) "24"
- 5) "sex"
- 6) "man"
- 192.168.1.209:6379>