<s> from __future__ import absolute_import , division , print_function , with_statement <EOL> from torndsession . driver import SessionDriver <EOL> import redis <EOL> from copy import copy <EOL> from datetime import timedelta , datetime <EOL> try : <EOL> import cPickle as pickle <EOL> except : <EOL> import pickle <EOL> class RedisSession ( SessionDriver ) : <EOL> """<STR_LIT>""" <EOL> def get ( self , session_id ) : <EOL> self . __create_redis_client ( ) <EOL> session_data = self . client . get ( session_id ) <EOL> if not session_data : return { } <EOL> return pickle . loads ( session_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> session_data = pickle . dumps ( session_data ) <EOL> self . __create_redis_client ( ) <EOL> self . client . set ( session_id , session_data ) <EOL> if expires : <EOL> delta_seconds = int ( ( expires - datetime . utcnow ( ) ) . total_seconds ( ) ) <EOL> self . client . expire ( session_id , delta_seconds ) <EOL> def clear ( self , session_id ) : <EOL> self . __create_redis_client ( ) <EOL> self . client . delete ( session_id ) <EOL> def remove_expires ( self ) : <EOL> pass <EOL> def __create_redis_client ( self ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> if '<STR_LIT>' in self . settings : <EOL> connection_pool = redis . ConnectionPool ( ** self . settings ) <EOL> settings = copy ( self . settings ) <EOL> del settings [ '<STR_LIT>' ] <EOL> settings [ '<STR_LIT>' ] = connection_pool <EOL> else : <EOL> settings = self . settings <EOL> self . client = redis . Redis ( ** settings ) </s>
