Normal session vs Memcachedsession vs Redissession

Source: Internet
Author: User
Tags sha1

I. Normal session (data stored in memory)
#!/usr/bin/env python#-*-coding:utf-8-*-from hashlib import sha1import osimport timeimport jsoncreate_session_id = Lamb Da:sha1 (bytes ('%s%s '% (os.urandom (+), Time.time ()), encoding= ' Utf-8 '). Hexdigest ()
Class Cachesession:session_container = {} session_id = "__sessionid__" def __init__ (self, handler): Self. Handler = Handler Client_random_str = Handler.get_cookie (cachesession.session_id, None) if Client_random_str and client_random_str in CacheSession.session_container:self.random_str = Client_random_str else: Self.random_str = create_session_id () cachesession.session_container[self.random_str] = {} expire S_time = time.time () + CONFIG. Session_expires #超时时间 Handler.set_cookie (cachesession.session_id, Self.random_str, Expires=expires_time) def _ _getitem__ (self, key): ret = Cachesession.session_container[self.random_str].get (key, None) return ret de F __setitem__ (self, Key, value): Cachesession.session_container[self.random_str][key] = value def __delitem__ (SE LF, key): If key in Cachesession.session_container[self.random_str]: del Cachesession.session_contAiner[self.random_str][key]
Second, the session is stored in the cache (Memcache)
#!/usr/bin/env python#-*-coding:utf-8-*-import configfrom hashlib import sha1import osimport timeimport jsonimport MEMC Acheconn = Memcache. Client ([' 192.168.11.119:12000 '], debug=true, cache_cas=true) create_session_id = lambda:sha1 (bytes ('%s%s '% (  Os.urandom (+), Time.time ()), encoding= ' Utf-8 ')). Hexdigest () class memcachedsession:session_id = "__sessionid__" def __init__ (self, handler): Self.handler = handler # get random string from client Client_random_str = Handler.get_cookie (memcachedsession.session_id, None) # If you get a random string from the client if Client_random_str and Conn.get (Client_rando            M_STR): Self.random_str = client_random_str else:self.random_str = create_session_id () Conn.set (Self.random_str, Json.dumps ({}), CONFIG. Session_expires) #config. Session_expires The timeout time set in the matching file Conn.set (self.random_str, Conn.get (self.random_str), config. Session_expires) Expires_time = time.time () + CONFIG. Session_expires haNdler.set_cookie (memcachedsession.session_id, Self.random_str, Expires=expires_time) def __getitem__ (self, key): ret = Conn.get (self.random_str) ret_dict = json.loads (ret) result = Ret_dict.get (Key,none) return re        Sult def __setitem__ (self, Key, value): ret = Conn.get (self.random_str) ret_dict = json.loads (ret) Ret_dict[key] = value Conn.set (self.random_str, Json.dumps (ret_dict), config.        Session_expires) def __delitem__ (self, key): ret = Conn.get (self.random_str) ret_dict = json.loads (ret) Del Ret_dict[key] Conn.set (Self.random_str, Json.dumps (ret_dict), config. Session_expires)
Third, the session is stored in the cache (Redis)
#!/usr/bin/env python#-*-coding:utf-8-*-import configfrom hashlib import sha1import osimport timeimport jsonimport Redi Spool = Redis. ConnectionPool (host= ' 192.168.11.119 ', port=6379) R = Redis. Redis (connection_pool=pool) create_session_id = lambda:sha1 (bytes ('%s%s '% (os.urandom (+), Time.time ()), encoding= ' Utf-8 '). Hexdigest () class redissession:session_id = "__sessionid__" def __init__ (self, handler): Self.handle R = Handler # get random string from client Client_random_str = Handler.get_cookie (redissession.session_id, None) # if from        The client obtains the random string if Client_random_str and R.exists (client_random_str): Self.random_str = Client_random_str Else:self.random_str = create_session_id () r.hset (Self.random_str,none,none) R.expire ( Self.random_str, CONFIG. session_expires) #设置超时时间 expires_time = time.time () + CONFIG. Session_expires Handler.set_cookie (redissession.session_id, Self.random_str, expires=expires_time) dEF __getitem__ (Self, key): result = R.hget (self.random_str,key) If result:ret_str = str (result, encoding= ' utf-8 ') Try:result = Json.loads (ret_str) Except:result = R ET_STR return result Else:return result def __setitem__ (self, Key, value): if Type (value) = = Dict:r.hset (self.random_str, Key, Json.dumps (value)) Else:r.hset (self.random_str , key, value) def __delitem__ (self, Key): R.hdel (Self.random_str,key)

Normal session vs Memcachedsession vs Redissession

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.