python operation Redis (Continued)

#!/usr/local/bin/python3.7

import redis
from redis import ConnectionPool

# General connection 
# Connect = redis.Redis (Host = '127.0.0.1', Port = 6379) 
# connect.set ( 'name', 'mozili') 
# Print (connect.get ( 'name'))

# Connection pool 
the POOL = the ConnectionPool (Host = ' 127.0.0.1 ' , Port = 6379, max_connections = 100 )
connect = redis.Redis(connection_pool=POOL)
set (name, value) to the name assigned value
connect.set ( ' name ' , ' mozili ' )
 # GET (name) Returns the string value in the name of the database 
Print (connect.get ( ' name ' ))
 # to the database key value assigned to a string name and returns last value 
Print (connect.getset ( ' name ' , ' limozi ' ))
 # returns the key corresponding to a plurality of value 
Print (connect.mget ([ ' name ' , ' Sex ' , ' Age ' ]))
 #If the key does not exist before the set value, the absence returns true, the presence of return to false 
Print (connect.setnx ( ' name ' , ' mozili ' )) # to false 
Print (connect.setnx ( ' newname ' , ' limozi ' )) # to true 
# set the value of the string type may correspond to a value, corresponding to this key and specifies Validity 
Print (connect.setex ( ' name ' ,. 3, ' mozili ' ))   # 3s later expired, key becomes absent 
# settings specify substring value of key values 
Print (connect.setrange ( ' name' ,. 6, ' Hello ' )) # Key name length value to value becomes 11 
# bulk does not return true copy duplicate key, repeatedly returns to false 
Print (connect.mset ({ ' NAME1 ' : ' zhangsan ' , ' NAME2 ' : ' Lisi ' })) # returns to true 
# Key not exist if the bulk copy 
Print (connect.msetnx ({ ' NAME3 ' : ' wangwu ' , ' NAME4 ' : ' zhaoliu '})) # Returns true



# URL database connected to embodiment 
# the TCP connection 
# Redis: // [: password], @host: Port / DB 
# the Redis the TCP connection the SSL +    
# rediss: // [: password], @host: Port / DB    
# the Redis the Unix the Socket connection 
# UNIX: // [: password] @ / path / to / socket.sock db = db?

Note:

1, the operation result can view the operation result redis visualizer

 

Guess you like

Origin www.cnblogs.com/lxmtx/p/12622493.html