Learning redis (key) key, Python redis key operation (b)

# -*- coding: utf-8 -*-
import redis
# Redis this connection can not be used, please modify according to their needs
r =redis.Redis(host="123.516.174.910",port=6379,password="11111608")
1. delete DEL command to delete keys that already exists. Non-existent key will be ignored
Copy the code
print r.set ( '1', '4028b2883d3f5a8b013d57228d760a93') #set value specified key, and returns True if the setting is correct

print r.get ( '1') # key to obtain a value of 4028b2883d3f5a8b013d57228d760a93 1

print r.delete ( '1') # 1 Delete key value

print r.get ( '1') # as above has a value of 1, the key removed, it returns None
Copy the code
2. exists
# 2 is set to the value of the key is 4028b2883d3f5a8b013d57228d760a93
r.set('2', '4028b2883d3f5a8b013d57228d760a93')
# Returns True existence does not exist returns False
print r.exists ( '2') # returns True
print r.exists ('33 ') # returns False
3. Expire command to set the expiration time for the key. key will expire no longer available.
r.set('2', '4028b2883d3f5a8b013d57228d760a93')
# Returns True success failure returns False, below 20 indicates 20 seconds
print r.expire('2',20)
# If we fail right time to get the key to a value of 2, whether the person is None
print r.get('2')
4. Expireat formatting command key to UNIX timestamp (unix timestamp) expiration time. key will expire no longer available. Idea: accurate to the second time, a digital time stamp is 10
r.set('2', '4028b2883d3f5a8b013d57228d760a93')
# Returns True success failure returns False, the following representation is 1598033936 2 expired in 2020-08-22 02:18:56 key
print r.expireat('2',1598033936)
print r.get('2')

 5.PEXPIREAT command sets the expiration time of the key, has milliseconds TECHNOLOGY. key will expire no longer available. Idea: millisecond accurate time, the time stamp is a 13-digit

r.set('2', '4028b2883d3f5a8b013d57228d760a93')
# Returns True success failure returns False.
print r.expireat('2',1598033936000)
print r.get('2')
6. Keys command is used to find all the key match a given pattern of pattern.
print r.set('111', '11')
print r.set('122', '12')
print r.set('113', '13')
print r.keys(pattern='11*')
# Result is outputted [ '113', '111'] and 11 because the key 122 does not match *
7. MOVE command for moving the current database key to which a given database db, can be set to select the current database, if necessary, see the select command 
because the database, we assume that DB0, we can use the following commands the key 2 to move into the database 1
r.move(2,1)
8. PERSIST command is used to remove the expiration time of a given key, such that the key never expire
Copy the code
# 1 is set to a value of 11 key
print r.set('1', '11')
Expiration time set key # 1 to 100 seconds
print r.expire(1,100)
# 1 key expiration time view of how much is left
print r.ttl('1')
# Object 13 seconds after removal of a key expiration time
import time
time.sleep(3)
# 1 key expiration time view of how much is left
print r.ttl('1')
# Remove the key expiration time of 1
r.persist(1)
# Check the expiration time a key is how much is left output None, we can see the expiration date through the key 1 redis desktop manager
print r.ttl('1')
Copy the code

9. Pttl milliseconds return command key remaining expiration time.

Copy the code
When the # key does not exist, returns -2. When the key is present but the remaining lifetime is not provided, -1. Otherwise, in milliseconds, return key remaining lifetime.
# 1 is set to a value of 11 key
print r.set('1', '11')
Expiration time set key # 1 to 100 seconds
print r.expire(1,100)
import time
time.sleep(3)
# 96994 returns the result, the result of the operation is not fixed, then 97 seconds large, the idea is to show the result returned is in milliseconds, one second is equal to 1000 milliseconds
print r.pttl('1')
Copy the code

10. TTL command return key seconds remaining expiration time.

Copy the code
When the # key does not exist, returns -2. When the key is present but the remaining lifetime is not provided, -1. Otherwise, in milliseconds, return key remaining lifetime.
# 1 is set to a value of 11 key
print r.set('1', '11')
print r.expire (1,100) # 1 setting key expiration time 100 seconds
import time
time.sleep(3)
print r.ttl ( '1') # 97 returns the result of
print r.ttl ( '123') # key 123 does not exist as a result is returned None
Copy the code
11.RANDOMKEY command returns a random key from the current database. When the database is not empty, return a key. When the database is empty, returns nil.
print r.randomkey () # returned by the database is the default database key
12 .Rename command is used to change the name of the key. OK when prompted to rename success, failure, when an error is returned.
print r.rename (1,1111) # modified successfully return True
print r.rename (222,1111) # If there is no key modifications fail to return redis.exceptions.ResponseError: no such key
13. Renamenx command is used to modify the name of the new key in the key does not exist.
print r.exists (123123) # returns false
print r.renamenx (1111,123123) # successful return True
print r.renamenx (1111,123123) # Failure to return redis.exceptions.ResponseError: no such key
Type 14. Type command returns the value stored in the key
Copy the code
# Return key data types, data types are: none (key not present), String (String), List (lists), SET (set), zset (ordered set), the hash (hash table),
print r.set('1',"111111111")
print r.type ( '1') # return a result string

print r.sadd('2','222222222222')
print r.type ( '2') # return a result set

print r.lpush('3','33333333')
print r.type ( '3') # return a result list
Copy the code

 

 

 

command

description

Redis DEL command

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

Redis Dump command

Sequence of a given key, and return the value to be serialized.

Redis EXISTS 命令

Checks if the given key exists.

Redis Expire command

seconds given key set the expiration time.

Redis Expireat command

And the role of EXPIREAT EXPIRE similar, are used to set the expiration time for the key. Except that the time parameter EXPIREAT accepted command is a UNIX timestamp (unix timestamp).

Redis PEXPIREAT command

Setting key expiration time one hundred million in milliseconds.

Redis PEXPIREAT command

Set key expiration time stamp (unix timestamp) in milliseconds

Redis Keys command

Find all key match a given pattern (pattern) of.

Redis Move command

The key to move the current database for a given database db them.

Redis PERSIST command

Removes the key expiration time, key will endure.

Redis Pttl command

Milliseconds returns the remaining expiration time of the key.

Redis TTL command

In seconds, returned to the remaining lifetime of a given key (TTL, time to live).

Redis RANDOMKEY command

It returns a random key from the current database.

Redis Rename command

Modify the name of the key

Redis Renamenx command

Only when newkey does not exist, the key renamed newkey.

Redis Type Command

Type of return value of the stored key.

 

Guess you like

Origin www.cnblogs.com/blackball9/p/11970355.html