Super detailed Redis Key operation API, what? can not read it! You come hammer me

  • j3_liuliang
  • Redis commonly used API is the application scenario start series (key), if you find it useful, you can follow the blogger and update it from time to time!

One, key (key)

1.1 TYPE(type)

The Redis Type command is used to return the type of the value stored in the key.

语法

127.0.0.1:6379> TYPE KEY_NAME 

可用版本: >= 1.0.0

返回值

Returns the data type of the key, the data types are:

  • none (key does not exist)
  • string (string)
  • list (list)
  • set
  • zset (ordered set)
  • hash (hash table)

Case:

127.0.0.1:6379> set stringkey stringvalue		#设置String类型
OK
127.0.0.1:6379> type stringkey					#返回String
string
127.0.0.1:6379> lpush listkey list01 list02		#设置list类型
(integer) 2
127.0.0.1:6379> type listkey					#返回list
list
127.0.0.1:6379> sadd setkey setvalue			#设置set类型
(integer) 1
127.0.0.1:6379> type setkey						#返回set
set
127.0.0.1:6379> zadd zsetkey 1 "liuliang"		#设置zset类型
(integer) 1
127.0.0.1:6379> type zsetkey					#返回zset
zset
127.0.0.1:6379> hset hashkey name liuliang		#设置hash类型
(integer) 1
127.0.0.1:6379> type hashkey					#返回hash
hash

1.2 PEXPIREAT (pexpireat)

The Redis PEXPIREAT command is used to set the expiration time of the key 亿毫秒记. After the key expires, it will no longer be available.

语法

127.0.0.1:6379> PEXPIREAT KEY_NAME TIME_IN_MILLISECONDS_IN_UNIX_TIMESTAMP

可以版本:>= 1.0.0

返回值:Return 1 if set successfully. When the key does not exist or the expiration time cannot be set for the key (for example, you try to update the expiration time of the key in a version of Redis lower than 2.1.3), return 0.

案例

127.0.0.1:6379> set k3 v3						#创建key 并赋值
OK
127.0.0.1:6379> pexpireat k3 5000000000000		#设置过期时间,大于等于这个输才行1000000000000
(integer) 1
127.0.0.1:6379> ttl k3							#查看剩余过期时间
(integer) 3398395525							#单位秒
127.0.0.1:6379> ttl k3
(integer) 3398395522
127.0.0.1:6379> 

1.3 RENAME(rename)

The Redis RENAME command is used to modify the name of the key.

语法

127.0.0.1:6379> RENAME OLD_KEY_NAME NEW_KEY_NAME

可以版本:>= 1.0.0

返回值:

It prompts OK when the name is changed successfully, and returns an error when it fails.

When OLD_KEY_NAME and NEW_KEY_NAME are the same, or OLD_KEY_NAME does not exist, an error is returned. When NEW_KEY_NAME already exists, the RENAME command will overwrite the old value.

案例

###################   第一种情况:重命名的新key不存在   ###################

127.0.0.1:6379> set j3-liuliang 18				#设置key并赋值
OK
127.0.0.1:6379> rename j3-liuliang liuliang		#重命名key,新key不存在
OK
127.0.0.1:6379> exists j3-liuliang				#exists判断key是否存在,0不存在,1存在
(integer) 0
127.0.0.1:6379> exists liuliang
(integer) 1

###################   第二种情况:重命名的新key存在   ###################

127.0.0.1:6379> set xiaozhang 28				#设置key并赋值
OK
127.0.0.1:6379> rename liuliang xiaozhang		#重命名key(liuliang),新key(xiaozhang)存在
OK												#重命名成功
127.0.0.1:6379> exists liuliang					#查看key是否存在
(integer) 0
127.0.0.1:6379> exists xiaozhang
(integer) 1
127.0.0.1:6379> get xiaozhang					#可以发现,如果重命名的key存在,则会覆盖新key的值
"18"

1.4 PERSIST (persist)

The Redis PERSIST command is used to remove the expiration time of a given key so that the key never expires.

语法

127.0.0.1:6379> PERSIST KEY_NAME

可以版本:>= 2.2.0

返回值:When the expiration time is successfully removed, 1 is returned. If the key does not exist or the key does not have an expiration time set, 0 is returned.

案例

127.0.0.1:6379> set j3-liuliang 18			#设置key并赋值
OK
127.0.0.1:6379> expire j3-liuliang 100		#设置过期时间
(integer) 1
127.0.0.1:6379> ttl j3-liuliang				#查看剩余过期时间
(integer) 98
127.0.0.1:6379> persist j3-liuliang			#移除key的过期时间
(integer) 1
127.0.0.1:6379> ttl j3-liuliang				#查看key状态,-1永久有效
(integer) -1

1.5 MOVE(move)

The Redis MOVE command is used to move the key of the current database to the given database db.

语法

127.0.0.1:6379> MOVE KEY_NAME DESTINATION_DATABASE

可以版本:>= 1.0.0

返回值:Return 1 if the move is successful, and 0 if it fails.

案例

###################   第一种情况:key存在于当前数据库中(移动成功,当前库不存在key目标库存在key)   ###################
127.0.0.1:6379> select 0					#切换数据库,默认就是 0 号数据库
OK
127.0.0.1:6379> set j3-liuliang 18			#设置key并复制
OK
127.0.0.1:6379> move j3-liuliang 1			#移动key到 1 号数据库
(integer) 1
127.0.0.1:6379> exists j3-liuliang			#查看当前数据库是否存在key
(integer) 0
127.0.0.1:6379> select 1					#切换数据库
OK
127.0.0.1:6379[1]> exists j3-liuliang		#查看当前数据库是否存在key
(integer) 1
127.0.0.1:6379[1]> 

###################   第二种情况:key不存在于当前数据库中(移动失败,因为key根本不存在)   #################
127.0.0.1:6379> flushall					#清空所有数据库数据 0-15
OK
127.0.0.1:6379> exists j3-liuliang			#查看当前数据库是否存在key
(integer) 0
127.0.0.1:6379> move j3-liuliang 1			#将一个不存在的key移动到 1 号数据库
(integer) 0									#返回 0 说明已经失败了
127.0.0.1:6379> select 1					#切换数据库
OK
127.0.0.1:6379[1]> exists j3-liuliang		#查看当前数据库是否存在key
(integer) 0									#显然不存在
127.0.0.1:6379[1]> 

###################   第三种情况:当源数据库和目标数据库有相同的 key 时(结论移动失败,不做任何变化)   ####
127.0.0.1:6379[1]> flushall					#清空所有数据库数据 0-15
OK
127.0.0.1:6379[1]> set j3-liuliang 28		#在当前数据库设置一个key
OK
127.0.0.1:6379[1]> select 0					#切换数据库
OK
127.0.0.1:6379> set j3-liuliang 18			#设置和上一个数据库中相同的一个key,值不要一样,便于区分
OK
127.0.0.1:6379> move j3-liuliang 1			#将当前数据库中key移动到上一个数据库中
(integer) 0									#发现,移动失败,说明,当两个数据库中有相同的key时,会移动失败
127.0.0.1:6379> get j3-liuliang				#获取当前数据库中key,发现没有变化
"18"
127.0.0.1:6379> select 1					#切换数据库
OK
127.0.0.1:6379[1]> get j3-liuliang			#发现也没有变化
"28"
127.0.0.1:6379[1]> 

1.6 RANDOMKEY(randomkey)

The Redis RANDOMKEY command randomly returns a key from the current database.

语法

127.0.0.1:6379> RANDOMKEY 

可以版本:>= 1.0.0

返回值:When the database is not empty, a key is returned. When the database is empty, nil is returned.

案例

127.0.0.1:6379> flushall								#清空所有数据库数据
OK
127.0.0.1:6379> mset k1 v1 k2 v2 k3 v3 k4 v4 k5 v5		#一次设置多个key value
OK
127.0.0.1:6379> keys *									#查看数据库说有key
1) "k4"
2) "k5"
3) "k1"
4) "k2"
5) "k3"
127.0.0.1:6379> randomkey								#随机输出一个key
"k4"
127.0.0.1:6379> randomkey
"k4"
127.0.0.1:6379> randomkey
"k4"
127.0.0.1:6379> randomkey
"k1"
127.0.0.1:6379> flushall								#情况所有数据库数据
OK
127.0.0.1:6379> randomkey								#发现输出为 nil
(nil)
127.0.0.1:6379> 

1.7 DUMP(dump)

The Redis DUMP command is used to serialize a given key and return the serialized value.

语法

127.0.0.1:6379> DUMP KEY_NAME

可以版本:>= 2.6.0

返回值:If the key does not exist, then nil is returned. Otherwise, return the serialized value.

案例

127.0.0.1:6379> flushall					#清空所有数据库
OK
127.0.0.1:6379> set j3-liuliang 18			#设置key并赋值
OK
127.0.0.1:6379> dump j3-liuliang			#将给定的key序列化
"\x00\xc0\x12\t\x00\x9d+/\x83A\xa7'\x9a"	#key序列后的值
127.0.0.1:6379> dump not-exists-key			#序列化不存在的值
(nil)										#返回nil
127.0.0.1:6379> 

1.8 TTL(ttl)

The Redis TTL command returns the remaining expiration time of the key in seconds.

语法

127.0.0.1:6379> TTL KEY_NAME

可以版本:>= 1.0.0

返回值:

When the key does not exist, -2 is returned. When the key exists but the remaining lifetime is not set, -1 is returned. Otherwise, return the remaining survival time of the key in milliseconds.

**Note: **Before Redis 2.8, when the key does not exist, or the key does not set the remaining time to live, the command will return -1.

案例

127.0.0.1:6379> flushall					#清空所有数据库数据
OK
127.0.0.1:6379> ttl j3-liuliang				#判断一个不存在的key
(integer) -2
127.0.0.1:6379> set j3-liuliang 18			#设置key并赋值
OK
127.0.0.1:6379> ttl j3-liuliang				#查看存在的key但没有设置过期时间
(integer) -1
127.0.0.1:6379> expire j3-liuliang 100		#给存在的key设置过期时间
(integer) 1
127.0.0.1:6379> ttl j3-liuliang				#查看剩余过期时间(秒)
(integer) 98
127.0.0.1:6379> 

1.9 EXPIRE(expire)

The Redis Expire command is used to set the expiration time of the key. After the key expires, it will no longer be available.

语法

127.0.0.1:6379> Expire KEY_NAME TIME_IN_SECONDS

可以版本:>= 1.0.0

返回值:

Return 1 if set successfully. When the key does not exist or the expiration time cannot be set for the key (for example, you try to update the expiration time of the key in a version of Redis lower than 2.1.3), return 0.

案例

127.0.0.1:6379> flushall					#清空所有数据库数据
OK
127.0.0.1:6379> set j3-liuliang 18			#设置key并赋值
OK
127.0.0.1:6379> expire j3-liuliang 100		#给存在的key设置过期时间
(integer) 1
127.0.0.1:6379> ttl j3-liuliang				#查看剩余过期时间(秒)
(integer) 88
127.0.0.1:6379> 

In the above example, we set the expiration time for the key j3-liuliang to 100 seconds, and the key will be automatically deleted after 100 seconds.

1.10 FROM (to)

The Redis DEL command is used to delete an existing key. Keys that do not exist will be ignored.

语法

127.0.0.1:6379> DEL KEY_NAME

可以版本:>= 1.0.0

返回值:The number of deleted keys.

案例

127.0.0.1:6379> flushall		#清空所有数据库数据
OK
127.0.0.1:6379> set k1 v1		#设置key并赋值
OK
127.0.0.1:6379> del k1			#删除key
(integer) 1						#返回删除的数量
127.0.0.1:6379> del k2			
(integer) 0						#返回删除的数量 0 说明没有改变什么,表明该值不存在
127.0.0.1:6379> 

1.11 PTTL(pttl)

Redis PTTL command 毫秒units return key remaining expiration time.

语法

127.0.0.1:6379> PTTL KEY_NAME

可以版本:>= 2.6.0

返回值:

When the key does not exist, -2 is returned. When the key exists but the remaining lifetime is not set, -1 is returned. Otherwise, return the remaining survival time of the key in milliseconds.

**Note: **Before Redis 2.8, when the key does not exist, or the key does not set the remaining time to live, the command will return -1.

案例

127.0.0.1:6379> flushall					#清空所有数据库数据
OK
127.0.0.1:6379> set j3-liuliang 18			#设置key并赋值
OK
127.0.0.1:6379> expire j3-liuliang 100		#设置过期时间
(integer) 1
127.0.0.1:6379> pttl j3-liuliang			#返回key的剩余时间,毫秒
(integer) 91266
127.0.0.1:6379> pttl key					#查看不存在key的剩余时间,毫秒
(integer) -2
127.0.0.1:6379> set k1 v1					#设置key并赋值
OK
127.0.0.1:6379> pttl k1						#查看没有设置过期时间的key
(integer) -1
127.0.0.1:6379> 

1.12 RENAMENX(renamenx)

The Redis Renamenx command is used to modify the name of the key when the new key does not exist.

语法

127.0.0.1:6379> RENAMENX OLD_KEY_NAME NEW_KEY_NAME

可以版本:>= 1.0.0

返回值:When the modification is successful, 1 is returned. If NEW_KEY_NAME already exists, return 0.

案例

###################   第一种情况:newkey不存在(改名成功)   ###################
127.0.0.1:6379> flushall						#清空数据库
OK
127.0.0.1:6379> set j3-liuliang 18				#设置key并赋值
OK
127.0.0.1:6379> renamenx j3-liuliang liuliang	#将key设置新值,新值不存在
(integer) 1
127.0.0.1:6379> exists j3-liuliang				#判断老key是否还存在
(integer) 0
127.0.0.1:6379> get j3-liuliang					#判断是否还可以取到老key的值
(nil)
127.0.0.1:6379> exists liuliang					#判断新key是否存在
(integer) 1
127.0.0.1:6379> get liuliang					#获取新key的值
"18"
127.0.0.1:6379>

###################   第二种情况:newkey存在(改名失败,不做变化)   ###################
127.0.0.1:6379> flushall			#清空数据库
OK
127.0.0.1:6379> set k1 v1			#设置key并赋值
OK
127.0.0.1:6379> set k2 v2
OK
127.0.0.1:6379> renamenx k1 k2		#将key设置新值,新值存在
(integer) 0							#重命名失败
127.0.0.1:6379> get k1				#没有任何变化
"v1"
127.0.0.1:6379> get k2				#没有任何变化
"v2"
127.0.0.1:6379>

1.13 EXISTS(exists)

The Redis EXISTS command is used to check whether the given key exists.

语法

127.0.0.1:6379> EXISTS KEY_NAME

可以版本:>= 1.0.0

返回值:If the key exists, return 1; otherwise, return 0.

案例

127.0.0.1:6379> flushall		#清空数据库
OK
127.0.0.1:6379> exists k1		#判断key是否存在
(integer) 0						#不存在
127.0.0.1:6379> set k1 v1		#设置key并赋值
OK
127.0.0.1:6379> exists k1		#判断key是否存在
(integer) 1						#存在
127.0.0.1:6379>

1.14 EXPIREAT(expireat)

EXPIREAT is similar to EXPIRE, and both are used to set the expiration time for the key. The difference is that the time parameter accepted by the EXPIREAT command is UNIX timestamp.

The Redis Expireat command is used to set the expiration time of the key in the UNIX timestamp format. After the key expires, it will no longer be available.

语法

127.0.0.1:6379> Expireat KEY_NAME TIME_IN_UNIX_TIMESTAMP

可以版本:>= 1.0.0

返回值:

Return 1 if set successfully. When the key does not exist or the expiration time cannot be set for the key (for example, you try to update the expiration time of the key in a version of Redis lower than 2.1.3), return 0.

案例

127.0.0.1:6379> flushall							#清空数据库
OK
127.0.0.1:6379> set j3-liuliang 18					#设置key并赋值
OK
127.0.0.1:6379> expireat j3-liuliang 1293840000		#给key设置过期时间,时间是时间戳
(integer) 1
127.0.0.1:6379> ttl j3-liuliang						#查看剩余过期时间
(integer) -2
127.0.0.1:6379> exists j3-liuliang					#查看key是否还存在
(integer) 0
127.0.0.1:6379> 

1.15 KEYS(keys)

The Redis Keys command is used to find all keys that match a given pattern. .

语法

127.0.0.1:6379> KEYS PATTERN

可以版本:>= 1.0.0

返回值:A list of keys (Array) that conform to the given pattern.

案例

127.0.0.1:6379> flushall		#清空数据库
OK
127.0.0.1:6379> set k1 v1		#创建一些key
OK
127.0.0.1:6379> set k2 v2
OK
127.0.0.1:6379> set m1 n1
OK
127.0.0.1:6379> set m2 n2
OK
127.0.0.1:6379> keys k*			#查找所有符合 k* 的key
1) "k1"
2) "k2"
127.0.0.1:6379> keys m*			#查找所有符合 m* 的key
1) "m2"
2) "m1"
127.0.0.1:6379> keys *			#查找所有的key
1) "m2"
2) "k1"
3) "m1"
4) "k2"
127.0.0.1:6379> 

Concluding remarks

  • This article is written in conjunction with the practice cases of Redis Chinese network and bloggers, and the String type will be written in the next issue
  • As the bloggers are not very knowledgeable, there will inevitably be mistakes. If you find a mistake or prejudice, please leave a message to point it out, and I will correct it.
  • If you think the article is not bad, your forwarding, sharing, liking, and commenting are your greatest encouragement.
  • Thank you for reading, welcome and thank you for your attention.

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_40399646/article/details/108906065