“This is the 19th day of my participation in the First Challenge 2022. For details: First Challenge 2022”


  • j3_liuliang
  • Redis common API is the application scenario start series (key), if you feel useful can pay attention to the blogger, not regularly update oh!

Related articles navigation

  1. Hyperdetailed Redis Key operation API, what? Look not to understand! You hammer me
  2. Redis String ultra detailed API usage and application scenarios introduction
  3. SCAN and related SSCAN, HSCAN, and ZSCAN commands are parsed
  4. What? The List type of Redis will not be used, see my super detailed API usage and application scenarios
  5. Redis Hash API usage and application scenarios
  6. Redis Set data type API use and illustrated application scenarios, do not see blood loss!
  7. Redis Sorted Set data type API and application scenario parsing

One, the key (key)

1.1 TYPE (TYPE)

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

Grammar:

127.0.0.1:6379 > TYPE KEY_NAMECopy the code

Available version: >= 1.0.0

The return value

Return the data type of key. The data types are:

  • None (key does not exist)
  • String (string)
  • List (list)
  • Set
  • Zset (ordered set)
  • Hash table

Case study:

127.0.0.1:6379 >set stringkey stringvalue		# set StringOK 127.0.0.1:6379 >type stringkey					# returns a StringString 127.0.0.1:6379> lPush ListKey List01 List02# set the list type
(integer6379 > 2 127.0.0.1) :type listkey					# to return to the listList 127.0.0.1:6379> sadd setkey setValue# set the set type
(integer6379 > 1 127.0.0.1) :type setkey						# to return to the set
set127.0.0.1:6379 > zadd zsetkey 1"liuliang"		Set the zset type
(integer6379 > 1 127.0.0.1) :type zsetkey					# returns zsetZset 127.0.0.1:6379> hset Hashkey name liuliang# Set the hash type
(integer6379 > 1 127.0.0.1) :type hashkey					# returns a hash
hash
Copy the code

1.2 PEXPIREAT (PEXPIREAT)

The Redis PEXPIREAT command is used to set the expiration time of a key in milliseconds. The key is no longer available after it expires.

grammar

127.0.0.1:6379 > PEXPIREAT KEY_NAME TIME_IN_MILLISECONDS_IN_UNIX_TIMESTAMPCopy the code

Version available: >= 1.0.0

Return value: 1 is returned on success. Returns 0 if the key does not exist or if you cannot set the expiration time for the key (such as in Redis earlier than 2.1.3 when you try to update the expiration time for the key).

case

127.0.0.1:6379 >set k3 v3						Create a key and assign it
OK
127.0.0.1:6379> pexpireat k3 5000000000000		# set the expiration time to 1000000000000
(integer) 1
127.0.0.1:6379> ttl k3							Check the remaining expiration time
(integer) 3398395525							# unit of secondsTTL k3 (127.0.0.1:6379 >integer6379 > 3398395522 127.0.0.1) :Copy the code

1.3 RENAME (RENAME)

The Redis RENAME command is used to RENAME a key.

grammar

127.0.0.1:6379 > RENAME OLD_KEY_NAME NEW_KEY_NAMECopy the code

Version available: >= 1.0.0

The return value:

An OK message is displayed on success, and an error is returned on failure.

An error is returned if OLD_KEY_NAME is the same as NEW_KEY_NAME, or if OLD_KEY_NAME does not exist. When NEW_KEY_NAME already exists, the RENAME command overwrites the old value.

case

# # # # # # # # # # # # # # # # # # # in the first case: rename the new key does not exist # # # # # # # # # # # # # # # # # # #127.0.0.1:6379 >set j3-liuliang 18				Set key and assign
OK
127.0.0.1:6379> rename j3-liuliang liuliang		The new key does not existOK 127.0.0.1:6379 > exists j3 - liuliang#exists Checks whether the key exists. 0 does not exist and 1 does exist
(integer0 127.0.0.1:6379> exists liuliang (integer1)# # # # # # # # # # # # # # # # # # # the second case: rename the new key # # # # # # # # # # # # # # # # # # #127.0.0.1:6379 >set xiaozhang 28				Set key and assign
OK
127.0.0.1:6379> rename liuliang xiaozhang		# rename key (liuliang), new key (xiaozhang) exists
OK												The rename succeeded127.0.0.1:6379 > exists liuliangCheck whether the key exists
(integer0 127.0.0.1:6379> exists xiaozhang (integer) 1
127.0.0.1:6379> get xiaozhang					If the renamed key exists, the value of the new key will be overwritten
"18"
Copy the code

1.4 PERSIST

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

grammar

127.0.0.1:6379 > PERSIST KEY_NAMECopy the code

Available version: >= 2.2.0

Return value: 1 is returned when the expiration time is successfully removed. If the key does not exist or the expiration time is not set, 0 is returned.

case

127.0.0.1:6379 >set j3-liuliang 18			Set key and assign
OK
127.0.0.1:6379> expire j3-liuliang 100		# Set expiration time
(integer) 1
127.0.0.1:6379> ttl j3-liuliang				Check the remaining expiration time
(integer) 98
127.0.0.1:6379> persist j3-liuliang			# Remove key expiration time
(integer) 1
127.0.0.1:6379> ttl j3-liuliang				-1 is always valid
(integer1) -Copy the code

1.5 MOVE (MOVE)

The Redis MOVE command is used to MOVE the key of the current database to the specified database.

grammar

127.0.0.1:6379 > MOVE KEY_NAME DESTINATION_DATABASECopy the code

Version available: >= 1.0.0

Return value: 1 on success, 0 on failure.

case

# # # # # # # # # # # # # # # # # # # in the first case: the key exists in the current database (mobile is successful, the current library does not exist the key target inventory in key) # # # # # # # # # # # # # # # # # # #127.0.0.1:6379 > select 0Mysql > alter database 0OK 127.0.0.1:6379 >set j3-liuliang 18			Set key and copy
OK
127.0.0.1:6379> move j3-liuliang 1			Move key to database 1
(integer) 1
127.0.0.1:6379> exists j3-liuliang			Check whether the current database has a key
(integer) 0
127.0.0.1:6379> select 1					Switch databaseOK 127.0.0.1:6379 [1] > exists j3 - liuliangCheck whether the current database has a key
(integer127.0.0.1) 1:6379 [1] ># # # # # # # # # # # # # # # # # # # 2: the key doesn't exist in the current database (mobile failure, because the key doesn't exist) # # # # # # # # # # # # # # # # #127.0.0.1:6379 > flushallClear all database data 0-15OK 127.0.0.1:6379 > exists j3 - liuliangCheck whether the current database has a key
(integer) 0
127.0.0.1:6379> move j3-liuliang 1			Move a key that does not exist to database 1
(integer) 0									# return 0 to indicate failure127.0.0.1:6379 > select 1Switch databaseOK 127.0.0.1:6379 [1] > exists j3 - liuliangCheck whether the current database has a key
(integer) 0									# Apparently does not exist127.0.0.1:6379 [1] ># # # # # # # # # # # # # # # # # # # 3: when the source database and target database have the same key (conclusion mobile failure, not to make any changes) # # # #127.0.0.1:6379 [1] > flushallClear all database data 0-15OK 127.0.0.1:6379 [1] >set j3-liuliang 28		Set a key to the current databaseOK 127.0.0.1:6379 [1] > select 0Switch databaseOK 127.0.0.1:6379 >set j3-liuliang 18			Mysql > select * from database where key = 'key'
OK
127.0.0.1:6379> move j3-liuliang 1			Move the key from the current database to the previous database
(integer) 0									If two databases have the same key, the database will fail to move127.0.0.1:6379 > get j3 - liuliangMysql > select * from key
"18"127.0.0.1:6379 > select 1Switch databaseOK 127.0.0.1:6379 [1] > get j3 - liuliang# No change in discovery
"28"127.0.0.1:6379 [1] >Copy the code

1.6 RANDOMKEY (RANDOMKEY)

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

grammar

127.0.0.1:6379 > RANDOMKEYCopy the code

Version available: >= 1.0.0

Return value: Returns a key if the database is not empty. Returns nil when the database is empty.

case

127.0.0.1:6379 > flushallClear all database data
OK
127.0.0.1:6379> mset k1 v1 k2 v2 k3 v3 k4 v4 k5 v5		# set multiple key values at onceOK 127.0.0.1:6379 > keys *SQL > select * from key
1) "k4"
2) "k5"
3) "k1"
4) "k2"
5) "k3"127.0.0.1:6379 > randomkeyPrint a random 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# Check all database dataOK 127.0.0.1:6379 > randomkey# Find output nil(nil) 127.0.0.1:6379 >Copy the code

1.7 DUMP (DUMP)

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

grammar

127.0.0.1:6379 > DUMP KEY_NAMECopy the code

Available version: >= 2.6.0

Return value: If key does not exist, return nil. Otherwise, the serialized value is returned.

case

127.0.0.1:6379 > flushallClear all databasesOK 127.0.0.1:6379 >set j3-liuliang 18			Set key and assignOK 127.0.0.1:6379 > dump j3 - liuliangSerialize the given key
"\x00\xc0\x12\t\x00\x9d+/\x83A\xa7'\x9a"	The value after the key sequence127.0.0.1:6379 > dump the not - the exists - keySerialize values that do not exist
(nil)										# returns nil127.0.0.1:6379 >Copy the code

1.8 TTL (TTL)

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

grammar

TTL KEY_NAME 127.0.0.1:6379 >Copy the code

Version available: >= 1.0.0

The return value:

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

** Note: ** Prior to Redis 2.8, commands returned -1 when the key did not exist, or when the key did not set the remaining lifetime.

case

127.0.0.1:6379 > flushallClear all database dataOK 127.0.0.1:6379 > TTL j3 - liuliang# determine a key that does not exist
(integer127.0.0.1) - 2:6379 >set j3-liuliang 18			Set key and assignOK 127.0.0.1:6379 > TTL j3 - liuliang# Check for existing keys but do not set expiration time
(integer) -1
127.0.0.1:6379> expire j3-liuliang 100		# set expiration time for existing keys
(integer) 1
127.0.0.1:6379> ttl j3-liuliang				# Check remaining expiration time (s)
(integer6379 > 98 127.0.0.1) :Copy the code

1.9 EXPIRE (EXPIRE)

The Redis Expire command is used to set the Expire time of a key. The key is no longer available after it expires.

grammar

127.0.0.1:6379 > Expire KEY_NAME TIME_IN_SECONDSCopy the code

Version available: >= 1.0.0

The return value:

If the setting succeeds, 1 is returned. Returns 0 if the key does not exist or if you cannot set the expiration time for the key (such as in Redis earlier than 2.1.3 when you try to update the expiration time for the key).

case

127.0.0.1:6379 > flushallClear all database dataOK 127.0.0.1:6379 >set j3-liuliang 18			Set key and assign
OK
127.0.0.1:6379> expire j3-liuliang 100		# set expiration time for existing keys
(integer) 1
127.0.0.1:6379> ttl j3-liuliang				# Check remaining expiration time (s)
(integer6379 > 88 127.0.0.1) :Copy the code

In the example above we set the expiration time for the key J3-LIUliang to 100 seconds after which the key will be deleted automatically.

1.10 DEL (DEL)

The Redis DEL command is used to delete an existing key. Non-existent keys are ignored.

grammar

127.0.0.1:6379 > DEL KEY_NAMECopy the code

Version available: >= 1.0.0

Returned value: The number of deleted keys.

case

127.0.0.1:6379 > flushallClear all database dataOK 127.0.0.1:6379 >set k1 v1		Set key and assignOK 127.0.0.1:6379 > del k1# delete key
(integer1)# return the number of deletions127.0.0.1:6379 > del k2 (integer) 0						What has not changed, indicating that the value does not exist127.0.0.1:6379 >Copy the code

1.11 PTTL (PTTL)

The Redis PTTL command returns the remaining expiration time of a key in milliseconds.

grammar

127.0.0.1:6379 > PTTL KEY_NAMECopy the code

Available version: >= 2.6.0

The return value:

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

** Note: ** Prior to Redis 2.8, commands returned -1 when the key did not exist, or when the key did not set the remaining lifetime.

case

127.0.0.1:6379 > flushallClear all database dataOK 127.0.0.1:6379 >set j3-liuliang 18			Set key and assign
OK
127.0.0.1:6379> expire j3-liuliang 100		# Set expiration time
(integer) 1
127.0.0.1:6379> pttl j3-liuliang			Return the remaining time of the key in milliseconds
(integer) 91266
127.0.0.1:6379> pttl key					# check the remaining time when the key does not exist, in milliseconds
(integer127.0.0.1) - 2:6379 >set k1 v1					Set key and assignOK 127.0.0.1:6379 > PTTL k1# check for keys that have not been set to expire
(integer127.0.0.1) - 1:6379 >Copy the code

1.12 RENAMENX (RENAMENX)

The Redis Renamenx command is used to change the name of a new key if it does not exist.

grammar

127.0.0.1:6379 > RENAMENX OLD_KEY_NAME NEW_KEY_NAMECopy the code

Version available: >= 1.0.0

Returned value: 1 is returned if the modification is successful. If NEW_KEY_NAME already exists, 0 is returned.

case

# # # # # # # # # # # # # # # # # # # in the first case: the newkey does not exist (renamed) # # # # # # # # # # # # # # # # # # #127.0.0.1:6379 > flushall# delete databaseOK 127.0.0.1:6379 >set j3-liuliang 18				Set key and assign
OK
127.0.0.1:6379> renamenx j3-liuliang liuliang	# set key to new value, new value does not exist
(integer) 1
127.0.0.1:6379> exists j3-liuliang				Check whether the old key still exists
(integer) 0
127.0.0.1:6379> get j3-liuliang					Check whether the old key can still be fetched(nil) 127.0.0.1:6379 > exists liuliangCheck whether the new key exists
(integer) 1
127.0.0.1:6379> get liuliang					Get the new key value
"18"127.0.0.1:6379 ># # # # # # # # # # # # # # # # # # # 2: the newkey exist (renamed failure, do not change) # # # # # # # # # # # # # # # # # # #127.0.0.1:6379 > flushall# delete databaseOK 127.0.0.1:6379 >set k1 v1			Set key and assignOK 127.0.0.1:6379 >setK2 v2 OK 127.0.0.1:6379> renamenx k1 k2Set key to new value, new value exists
(integer) 0							Failed to rename127.0.0.1:6379 > get k1# Nothing has changed
"v1"127.0.0.1:6379 > get k2# Nothing has changed
"v2"127.0.0.1:6379 >Copy the code

1.13 EXISTS (EXISTS)

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

grammar

127.0.0.1:6379 > EXISTS KEY_NAMECopy the code

Version available: >= 1.0.0

Return value: 1 if key exists, 0 otherwise.

case

127.0.0.1:6379 > flushall# delete databaseOK 127.0.0.1:6379 > exists k1Check whether the key exists
(integer) 0						# there is no127.0.0.1:6379 >set k1 v1		Set key and assignOK 127.0.0.1:6379 > exists k1Check whether the key exists
(integer1)# there127.0.0.1:6379 >Copy the code

1.14 EXPIREAT (EXPIREAT)

EXPIREAT is similar to EXPIRE in that it is used to set an expiration time for a key. The difference is that the EXPIREAT command accepts a UNIX timestamp as the time parameter.

The Redis Expireat command is used to set the expiration time of a key in UNIX timestamp format. The key is no longer available after it expires.

grammar

127.0.0.1:6379 > Expireat KEY_NAME TIME_IN_UNIX_TIMESTAMPCopy the code

Version available: >= 1.0.0

The return value:

If the setting succeeds, 1 is returned. Returns 0 if the key does not exist or if you cannot set the expiration time for the key (such as in Redis earlier than 2.1.3 when you try to update the expiration time for the key).

case

127.0.0.1:6379 > flushall# delete databaseOK 127.0.0.1:6379 >set j3-liuliang 18					Set key and assign
OK
127.0.0.1:6379> expireat j3-liuliang 1293840000		# set the expiration time for key
(integer) 1
127.0.0.1:6379> ttl j3-liuliang						Check the remaining expiration time
(integer) -2
127.0.0.1:6379> exists j3-liuliang					# check whether the key still exists
(integer) 0 127.0.0.1:6379 >Copy the code

1.15 KEYS (KEYS)

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

grammar

127.0.0.1:6379 > KEYS the PATTERNCopy the code

Version available: >= 1.0.0

Returned value: List of keys (Array) that match the given pattern.

case

127.0.0.1:6379 > flushall# delete databaseOK 127.0.0.1:6379 >set k1 v1		# Create some keysOK 127.0.0.1:6379 >setK2 v2 OK 127.0.0.1:6379 >setM1 n1 OK 127.0.0.1:6379 >setM2 n2 OK 127.0.0.1:6379> keys k*Find all keys that match k*
1) "k1"
2) "k2"127.0.0.1:6379 > keys m *Find all keys that match m*
1) "m2"
2) "m1"127.0.0.1:6379 > keys *Find all keys
1) "m2"
2) "k1"
3) "m1"
4) "k2"127.0.0.1:6379 >Copy the code

conclusion

  • This article combines Redis Chinese network and blogger’s practice case to write, the next period to write String type
  • Due to the lack of knowledge of the blogger, there will be mistakes, if you find mistakes or bias, please comment to me, I will correct it.
  • If you think the article is good, your retweets, shares, likes and comments are the biggest encouragement to me.
  • Thank you for reading. Welcome and thank you for your attention.