<s> from __future__ import absolute_import , division , print_function , with_statement <EOL> from torndsession . driver import SessionDriver <EOL> import memcache <EOL> import datetime <EOL> import numbers <EOL> from copy import copy <EOL> try : <EOL> import cPickle as pickle <EOL> except : <EOL> import pickle <EOL> """<STR_LIT>""" <EOL> class MemcachedSession ( SessionDriver ) : <EOL> """<STR_LIT>""" <EOL> DEFAULT_MEMCACHED_HOST = '<STR_LIT:127.0.0.1>' <EOL> DEFAULT_MEMCACHED_PORT = '<STR_LIT>' <EOL> def __init__ ( self , ** settings ) : <EOL> super ( MemcachedSession , self ) . __init__ ( ** settings ) <EOL> self . client = self . __create_memcached_client ( ) <EOL> def get ( self , session_id ) : <EOL> mem_data = self . client . get ( session_id ) <EOL> if not mem_data : return { } <EOL> return pickle . loads ( mem_data ) <EOL> def save ( self , session_id , session_data , expires = None ) : <EOL> session_data = session_data if session_data else { } <EOL> if expires : <EOL> session_data . update ( __expires__ = expires ) <EOL> mem_data = pickle . dumps ( session_data ) <EOL> expires = self . __get_expires_seconds ( expires ) <EOL> self . client . set ( session_id , mem_data , expires ) <EOL> def clear ( self , session_id ) : <EOL> self . client . delete ( session_id ) <EOL> def remove_expires ( self ) : <EOL> pass <EOL> def __create_memcached_client ( self ) : <EOL> settings = copy ( self . settings ) <EOL> host = settings . pop ( '<STR_LIT:host>' , self . DEFAULT_MEMCACHED_HOST ) <EOL> port = settings . pop ( '<STR_LIT:port>' , self . DEFAULT_MEMCACHED_PORT ) <EOL> servers = '<STR_LIT>' % ( host , port ) <EOL> return memcache . Client ( servers , ** settings ) <EOL> def __get_expires_seconds ( self , expires ) : <EOL> if isinstance ( expires , numbers . Real ) : <EOL> return int ( expires ) <EOL> elif isinstance ( expires , datetime . datetime ) : <EOL> now = datetime . datetime . utcnow ( ) <EOL> return int ( ( expires - now ) . total_seconds ( ) ) <EOL> else : <EOL> return <NUM_LIT:0> </s>
