<s> __author__ = '<STR_LIT>' <EOL> '''<STR_LIT>''' <EOL> import os , time , datetime , functools , logging <EOL> try : <EOL> import cPickle as pickle <EOL> except ImportError : <EOL> import pickle <EOL> class DummyClient ( object ) : <EOL> def set ( self , key , value , expires = <NUM_LIT:0> ) : <EOL> pass <EOL> def setint ( self , key , value , expires = <NUM_LIT:0> ) : <EOL> pass <EOL> def get ( self , key , default = None ) : <EOL> return default <EOL> def gets ( self , * keys ) : <EOL> return [ None ] * len ( keys ) <EOL> def getint ( self , key , default = <NUM_LIT:0> ) : <EOL> return default <EOL> def getints ( self , * keys ) : <EOL> return [ <NUM_LIT:0> ] * len ( keys ) <EOL> def delete ( self , key ) : <EOL> pass <EOL> def incr ( self , key ) : <EOL> pass <EOL> def decr ( self , key ) : <EOL> pass <EOL> class MemcacheClient ( object ) : <EOL> def __init__ ( self , servers , debug = False ) : <EOL> import memcache <EOL> if isinstance ( servers , basestring ) : <EOL> servers = [ servers ] <EOL> self . _client = memcache . Client ( servers , debug ) <EOL> def set ( self , key , value , expires = <NUM_LIT:0> ) : <EOL> '''<STR_LIT>''' <EOL> self . _client . set ( key , value , expires ) <EOL> setint = set <EOL> def get ( self , key , default = None ) : <EOL> '''<STR_LIT>''' <EOL> r = self . _client . get ( key ) <EOL> return default if r is None else r <EOL> def getint ( self , key , default = <NUM_LIT:0> ) : <EOL> '''<STR_LIT>''' <EOL> r = self . _client . get ( key ) <EOL> return _safe_int ( r , default ) <EOL> def gets ( self , * keys ) : <EOL> '''<STR_LIT>''' <EOL> r = self . _client . get_multi ( keys ) <EOL> return map ( lambda k : r . get ( k ) , keys ) <EOL> def getints ( self , * keys ) : <EOL> '''<STR_LIT>''' <EOL> r = self . _client . get_multi ( keys ) <EOL> return map ( lambda k : _safe_int ( r . get ( k ) ) , keys ) <EOL> def delete ( self , key ) : <EOL> '''<STR_LIT>''' <EOL> self . _client . delete ( key ) <EOL> def incr ( self , key ) : <EOL> '''<STR_LIT>''' <EOL> r = self . _client . incr ( key ) <EOL> if r is None : <EOL> self . _client . set ( key , <NUM_LIT:1> ) <EOL> r = <NUM_LIT:1> <EOL> return r <EOL> def decr ( self , key ) : <EOL> '''<STR_LIT>''' <EOL> r = self . _client . decr ( key ) <EOL> if r is None : <EOL> self . _client . set ( key , <NUM_LIT:0> ) <EOL> r = <NUM_LIT:0> <EOL> return r <EOL> def _safe_pickle_loads ( r ) : <EOL> if r is None : <EOL> return None <EOL> try : <EOL> return pickle . loads ( r ) <EOL> except pickle . UnpicklingError : <EOL> pass <EOL> return None <EOL> def _safe_int ( r , default = <NUM_LIT:0> ) : <EOL> if r is None : <EOL> return default <EOL> try : <EOL> return int ( r ) <EOL> except ValueError : <EOL> return default <EOL> class RedisClient ( object ) : <EOL> def __init__ ( self , servers , debug = False ) : <EOL> import redis <EOL> self . _client = redis . StrictRedis ( host = servers ) <EOL> def setint ( self , key , value , expires = <NUM_LIT:0> ) : <EOL> self . _set ( key , value , expires , use_pickle = False ) <EOL> def set ( self , key , value , expires = <NUM_LIT:0> ) : <EOL> '''<STR_LIT>''' <EOL> logging . debug ( '<STR_LIT>' % key ) <EOL> self . _set ( key , value , expires , use_pickle = True ) <EOL> def _set ( self , key , value , expires , use_pickle ) : <EOL> self . _client . set ( key , pickle . dumps ( value ) if use_pickle else value ) <EOL> if expires : <EOL> self . _client . expire ( key , expires ) <EOL> def get ( self , key , default = None ) : <EOL> '''<STR_LIT>''' <EOL> logging . debug ( '<STR_LIT>' % key ) <EOL> r = self . _client . get ( key ) <EOL> if r is None : <EOL> return default <EOL> return _safe_pickle_loads ( r ) <EOL> def gets ( self , * keys ) : <EOL> '''<STR_LIT>''' <EOL> return map ( _safe_pickle_loads , self . _client . mget ( keys ) ) <EOL> def delete ( self , key ) : <EOL> '''<STR_LIT>''' <EOL> self . _client . delete ( key ) <EOL> def getints ( self , * keys ) : <EOL> '''<STR_LIT>''' <EOL> return map ( _safe_int , self . _client . mget ( keys ) ) <EOL> def getint ( self , key , default = <NUM_LIT:0> ) : <EOL> return _safe_int ( self . _client . get ( key ) , default ) <EOL> def incr ( self , key ) : <EOL> '''<STR_LIT>''' <EOL> return self . _client . incr ( key ) <EOL> def decr ( self , key ) : <EOL> '''<STR_LIT>''' <EOL> return self . _client . decr ( key ) <EOL> client = DummyClient ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import uuid , doctest <EOL> doctest . testmod ( ) </s>
