Redis Learning One (key) key, Python operation Redis Key

Source: Internet
Author: User
Tags delete key set set redis desktop manager


#-*-coding: utf-8-*-
import redis
#This redis connection is not available, please modify it according to your needs
r = redis.Redis (host = "123.516.174.910", port = 6379, password = "11111608")
1. The delete DEL command is used to delete an existing key. Non-existent keys will be ignored
print r.set (‘1’, ‘4028b2883d3f5a8b013d57228d760a93’) #set Set the value of the specified key, and return True if the setting is correct

print r.get (‘1‘) # get the value with key 1 4028b2883d3f5a8b013d57228d760a93

print r.delete (‘1‘) # delete value with key 1

print r.get (‘1‘) #Because the value of key 1 has been deleted above, it returns None
2. exists
#Set the value of key 2 to 4028b2883d3f5a8b013d57228d760a93
r.set (‘2’, ‘4028b2883d3f5a8b013d57228d760a93’)
# True if it exists, false if it doesn't exist
print r.exists (‘2‘) #returns True
print r.exists (‘33 ‘) #returns False
3. The Expire command is used to set the key expiration time. After the key expires, it will no longer be available.
r.set (‘2’, ‘4028b2883d3f5a8b013d57228d760a93’)
#True on success, returns False on failure, the following 20 means 20 seconds
print r.expire (‘2‘, 20)
#If time is ok, we can get a value of 2 for the key, or None
print r.get (‘2‘)
 4. The Expireat command is used to set the expiration time of the key in UNIX timestamp format. After the key expires, it will no longer be available. Idea: Time is accurate to seconds, timestamp is 10 as a number
r.set (‘2’, ‘4028b2883d3f5a8b013d57228d760a93’)
#Return True on success, or False on failure. The following 1598033936 indicates that the key 2 expired on 2020-08-22 02:18:56
print r.expireat (‘2‘, 1598033936)
print r.get (‘2‘)
5.PEXPIREAT command is used to set the expiration time of key, it has been milliseconds. After the key expires, it will no longer be available. Idea: Time is accurate to milliseconds, timestamp is 13 digits

r.set (‘2’, ‘4028b2883d3f5a8b013d57228d760a93’)
#True on success, returns False on failure.
print r.expireat (‘2‘, 1598033936000)
print r.get (‘2‘)
6. The Keys command is used to find all keys that match the given pattern.
print r.set (‘111’, ‘11’)
print r.set (‘122’, ‘12’)
print r.set (‘113’, ‘13’)
print r.keys (pattern = ‘11 * ‘)
# The output is [‘113’, ‘111’] because key 122 does not match 11 *
7. The MOVE command is used to move the key of the current database to the given database db. Select can set the current database. If necessary, see the select command.
Because our default database is db0, we can use the following command key 2 to move to database 1
r.move (2,1)
8. The PERSIST command is used to remove the expiration time of a given key so that the key never expires
#Set the key to 1 and the value to 11
print r.set (‘1’, ‘11’)
#Set key 1 expiration time is 100 seconds
print r.expire (1,100)
# See how much time expires for key 1
print r.ttl (‘1‘)
# Purpose is to remove the expiration time of key 1 after 13 seconds
import time
time.sleep (3)
# See how much time expires for key 1
print r.ttl (‘1‘)
#Remove key 1 expiration time
r.persist (1)
# View the remaining time of the expiration time of key 1 The output is None, we can use the redis desktop manager to view the expiration time of key 1
print r.ttl (‘1‘)
9. The Pttl command returns the remaining expiration time of the key in milliseconds.

# When key does not exist, -2 is returned. When key exists but no remaining lifetime is set, -1 is returned. Otherwise, in milliseconds, the remaining lifetime of the key is returned.
#Set the key to 1 and the value to 11
print r.set (‘1’, ‘11’)
#Set key 1 expiration time is 100 seconds
print r.expire (1,100)
import time
time.sleep (3)
#The returned result is 96994. The running result is not fixed, it is greater than 97 seconds. The idea is to show that the returned result is milliseconds. One second equals 1000 milliseconds
print r.pttl (‘1‘)
10. The TTL command returns the remaining expiration time of the key in seconds.

# When key does not exist, -2 is returned. When key exists but no remaining lifetime is set, -1 is returned. Otherwise, in milliseconds, the remaining lifetime of the key is returned.
#Set the key to 1 and the value to 11
print r.set (‘1’, ‘11’)
print r.expire (1,100) #Set key 1 expiration time to 100 seconds
import time
time.sleep (3)
print r.ttl (‘1‘) #The result returned is 97
print r.ttl (‘123’) #Because key 123 does not exist, the result returned is None
11. The RANDOMKEY command randomly returns a key from the current database. When the database is not empty, a key is returned. When the database is empty, nil is returned.
print r.randomkey () #The database returns the default database key
12 .Rename command is used to modify the name of key. It will prompt OK when the name is changed successfully, and return an error when it fails.
print r.rename (1,1111) #Modify successfully and return True
print r.rename (222,1111) #If the key does not exist, the modification fails and returns redis.exceptions.ResponseError: no such key
13. The Renamenx command is used to modify the name of a key when a new key does not exist.
print r.exists (123123) #return false
print r.renamenx (1111,123123) #Return True on success
print r.renamenx (1111,123123) #returns redis.exceptions.ResponseError: no such key on failure
14. Type command is used to return the type of value stored by key
# Returns the data type of key. The data types are: none (key does not exist), string (string), list (list), set (set), zset (ordered set), hash (hash table),
print r.set (‘1’, "111111111")
print r.type (‘1‘) #The result returned is string

print r.sadd (‘2’, ‘222222222222’)
print r.type (‘2‘) #The result returned is set

print r.lpush (‘3’, ‘33333333’)
print r.type (‘3‘) #The result returned is list

Command

Describe

Redis DEL Command

This command is used to delete key in the presence of key.

Redis Dump Command

Serializes the given key and returns the serialized value.

Redis EXISTS Command

Checks if a given key exists.

Redis Expire Command

seconds sets the expiration time for a given key.

Redis expireat Command

The Expireat function is similar to EXPIRE and is used to set the expiration time for the key. The difference is that the time parameter accepted by the Expireat command is a UNIX timestamp (Unix timestamp).

Redis pexpireat Command

Set the expiration time of key to hundreds of milliseconds.

Redis pexpireat Command

Set the timestamp for key expiration Time (Unix timestamp) in milliseconds

Redis Keys Command

Finds all keys that match the given pattern (pattern).

Redis Move Command

Moves the key of the current database into the given DB.

Redis PERSIST Command

When the key expires, key will persist.

Redis pttl Command

Returns the remaining expiration time of key in milliseconds.

Redis TTL Command

Returns the remaining lifetime (TTL, Time to live) for a given key, in seconds.

Redis randomkey Command

Returns a key randomly from the current database.

Redis Rename Command

Change the name of the key

Redis Renamenx Command

Rename the key to Newkey only if the Newkey does not exist.

Redis Type Command

Returns the type of value stored by key.


Redis Learning One (key) key, Python operation Redis Key

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.