1.python Operation redis1.1. Installation
Enter the command:
Pip Install Redis
2.2. Use
Introducing ModulesImportRedis ConnectionTry: R=redis. Strictredis (host='localhost', port=6379) exceptexception,e:Printe.message Mode one: Depending on the data type, call the appropriate method, complete read and write more methods with the upper written command R.set ('name','Hello') R.get ('name ')mode two: Pipline buffer multiple commands, then execute one time, reduce the server-TCP database packets between clients, thereby improving the efficiency of the pipe=r.pipeline () pipe.set ('name',' World') Pipe.get ('name') Pipe.execute ()
#connection to Redis Server section is consistent#this encapsulates the read and write of a string typeImportRedisclassredishelper ():def __init__(self,host='localhost', port=6379): Self.__redis=Redis. Strictredis (host, Port)defGet (Self,key):ifSelf.__redis. Exists (key):returnSelf.__redis. Get (key)Else: return "" defSet (Self,key,value): Self.__redis. Set (Key,value)
2. Example
#Coding=utf-8ImportRedisclassCredis:def __init__(self): Self.host='localhost'Self.port= 6379self.db=0 SELF.R= Redis. Redis (host = self.host, port = self.port, db =self.db)#1. Strings Type and Operation #sets the value of the key corresponding to string type defset (self, key, value):returnSelf.r.set (key, value)#sets the value of the key corresponding to string type. If key already exists, return 0,nx is not exist meaning defsetnx (self, Key, value):returnself.r.setnx (key, value)#set the value of the key to a string type of value and specify the validity period for this key value defSetex (self, key, time, value):returnSelf.r.setex (key, time, value)#sets the substring of the value of the specified key #SetRange name 8 gmail.com #8 of these are the beginning of the substitution of characters from subscript 8 (containing 8) defsetrange (self, key, num, value):returnself.r.setrange (key, num, value)#gets the substring of the value of the specified key defGetRange (self, key, start, end):returnSelf.r.getrange (key, start, end)#mget (list) defget (self, key):ifisinstance (Key, list):returnself.r.mget (Key)Else: returnself.r.get (Key)#Delete defRemove (self, key):returnSelf.r.delete (Key)#Self-increment defINCR (self, key, default = 1): if(1 = =default):returnself.r.incr (Key)Else: returnself.r.incr (key, default)#Self-reduction defDECR (self, key, default = 1): if(1 = =default):returnSELF.R.DECR (Key)Else: returnSELF.R.DECR (key, default)#2. Hashes type and Operation #get session information by email defhget (self, email):returnSelf.r.hget ('Session', email)#Add user session with email as a unique identifier defHset (self, email, content):returnSelf.r.hset ('Session', email, content)#get all the data in the Session hash table defHgetall (self):returnSelf.r.hgetall ('Session') #Delete Hashes defHdel (self, name, key =None):if(key):returnSelf.r.hdel (name, key)returnSelf.r.hdel (name)#Empty Current DB defClear (self):returnself.r.flushdb ()#3. Lists type and Operation #Suitable for mail queues #adds a string element to the head of the list in key defLpush (self, Key, value):returnSelf.r.lpush (key, value)#deletes an element from the tail of the list and returns the element to delete defLpop (self, key):returnSelf.r.plush (Key)if __name__=='__main__': R= Credis ()
3.Redis cached MySQL Demo user login, Python implementation instance
- The overall logic is as follows:
1. User login first to determine whether in the Redis cache, if in the Redis cache, the direct login success;
2. If the user is not in the Redis cache, then access MySQL to determine whether the user exists, if not, prompt the user to register, if present, the login is successful;
3. While MySQL is present and logged in successfully, the stripe data is cached with the Redis hash type and the expiration time is set to 20 minutes;
- Python Operations Redis User Login
#-*-coding:utf-8-*-ImportRedisclassRedis_login ():def __init__(SELF,USER,PWD): R.mset (Guoyan='123', lixiang='1234', shengli='12345') defLogin (self): LS= [] forKeyinchR.keys (): Ls.append (Key.decode ('Utf-8')) ifUser not inchls:Print('User name Error Please re-enter') elifR.get (user). Decode ('Utf-8') ==pwd:Print('login success!!! ') Else: Red.not_login ()defNot_login (self):Print('input Error Please re-enter')if __name__=='__main__': Pool= Redis. ConnectionPool (host='127.0.0.1', port=6379) R= Redis. Redis (connection_pool=pool) whileTrue:user= Raw_input ('Please enter user name \ n') PWD= Raw_input ('Please enter the password \ n') Red=Redis_login (USER,PWD) red.login ()
Python Operation Redis