Sir you are finally back! Let us be guided by the spirit of (SI) Zuo session.py read it ...
First look at the libraries you need:
Pickle a library for serialization deserialization (not understood?) You can just as well act like JSON. )
HMAC and Hashlib used to generate an encrypted string
UUID is used to generate a unique ID
Memcache Python's Memcache client
There are three classes in this, Sessiondata Session and SessionManager. Let's look at the simplest sessiondata.
Sessiondata is used to store session data in a dictionary structure, inheriting from a dictionary, in fact, only two more member variables than the dictionary:
# Inherit the dictionary because the session access is similar to the dictionary class Sessiondata (dict): # Initialize with session ID and Hmac_key def __init__ (self, session_id, h Mac_key): self.session_id = session_id Self.hmac_key = Hmac_key
Then there is the real Session class. The session class inherits from the Sessiondata, noting that it is still much like a built-in type dictionary, just rewriting its own initialization method and defining the Save interface--to save the modified session data.
# Inherit Sessiondata class Session (sessiondata): # Initialize, bind Session_manager and tornado corresponding handler def __init__ (self, Sess Ion_manager, request_handler): Self.session_manager = Session_manager Self.request_handler = request_handler Try: # Normal is to get all the data of the session, save Current_session = Session_manager.get in sessiondata form (reques T_handler) except invalidsessionexception: # If the first access throws an exception, the exception is to get an empty Sessiondata object with no data, but contains the newly generated # session_id and Hmac_key current_session = Session_manager.get () # Take out the data in the current_session to the key value pair The form of the iteration is stored for key, the data in Current_session.iteritems (): Self[key] = data # saved under session_id SE lf.session_id = current_session.session_id # and corresponding Hmac_key self.hmac_key = current_session.hmac_key # definition The Save method, which is used for saving after session modification, actually calls the set method of Session_manager def Save (self): Self.session_manager.set (Self.request_h Andler, self)
__init__ methods are difficult to understand, the basic process is to define their own Session_manager and handler processing objects. Then through the Session_manager to obtain the existing session data, using this data to initialize a user access session, if the user is the first access, then he got a new Sessiondata object, because there may be new users, so here to session_id and Hmac_key (what ghosts) are assigned.
The Save method is to provide a saving interface to modify the session data, actually call the Session_manager set method, the implementation is not considered.
Seeing these two classes, you should have a basic understanding of the work of the session, which can be considered from the user's access process. Note Basehandler This entry, each user's access is an HTTP request. When the user first accesses or the last session expires, at this time when the user accesses tornado established a handler object (the handler must inherit from Basehandler), and at the time of initialization a session object was established, because is the new access, the In the current session there is no data, after the use of key/value pairs in the form of reading and writing session (do not forget the session has all the dictionary operation), modified by the Save method saved session. If the user is not a new access, then the process is followed, but the session is initialized with the previous data taken out to save in the instance. When the user ends the access, HTTP disconnects, handler instances are destroyed, session instances are destroyed (note that the instance is destroyed, not data destroyed).
is not feeling a bit dizzy .... Well, I'm dizzy, too. We'll see SessionManager after we understand it. This class is a little bit bigger ... Feed... Children's shoes don't go! There is really only one point left .... Feed....
Session support for Tornado with memcached (ii)