<s> """<STR_LIT>""" <EOL> from time import time as currentTime <EOL> from Cheetah . Utils . memcache import Client as MemcachedClient <EOL> class Error ( Exception ) : <EOL> pass <EOL> class AbstractCacheStore ( object ) : <EOL> def set ( self , key , val , time = None ) : <EOL> raise NotImplementedError <EOL> def add ( self , key , val , time = None ) : <EOL> raise NotImplementedError <EOL> def replace ( self , key , val , time = None ) : <EOL> raise NotImplementedError <EOL> def delete ( self , key ) : <EOL> raise NotImplementedError <EOL> def get ( self , key ) : <EOL> raise NotImplementedError <EOL> class MemoryCacheStore ( AbstractCacheStore ) : <EOL> def __init__ ( self ) : <EOL> self . _data = { } <EOL> def set ( self , key , val , time = <NUM_LIT:0> ) : <EOL> self . _data [ key ] = ( val , time ) <EOL> def add ( self , key , val , time = <NUM_LIT:0> ) : <EOL> if self . _data . has_key ( key ) : <EOL> raise Error ( '<STR_LIT>' % key ) <EOL> self . _data [ key ] = ( val , time ) <EOL> def replace ( self , key , val , time = <NUM_LIT:0> ) : <EOL> if self . _data . has_key ( key ) : <EOL> raise Error ( '<STR_LIT>' % key ) <EOL> self . _data [ key ] = ( val , time ) <EOL> def delete ( self , key ) : <EOL> del self . _data [ key ] <EOL> def get ( self , key ) : <EOL> ( val , exptime ) = self . _data [ key ] <EOL> if exptime and currentTime ( ) > exptime : <EOL> del self . _data [ key ] <EOL> raise KeyError ( key ) <EOL> else : <EOL> return val <EOL> def clear ( self ) : <EOL> self . _data . clear ( ) <EOL> class MemcachedCacheStore ( AbstractCacheStore ) : <EOL> servers = ( '<STR_LIT>' ) <EOL> def __init__ ( self , servers = None , debug = False ) : <EOL> if servers is None : <EOL> servers = self . servers <EOL> self . _client = MemcachedClient ( servers , debug ) <EOL> def set ( self , key , val , time = <NUM_LIT:0> ) : <EOL> self . _client . set ( key , val , time ) <EOL> def add ( self , key , val , time = <NUM_LIT:0> ) : <EOL> res = self . _client . add ( key , val , time ) <EOL> if not res : <EOL> raise Error ( '<STR_LIT>' % key ) <EOL> self . _data [ key ] = ( val , time ) <EOL> def replace ( self , key , val , time = <NUM_LIT:0> ) : <EOL> res = self . _client . replace ( key , val , time ) <EOL> if not res : <EOL> raise Error ( '<STR_LIT>' % key ) <EOL> self . _data [ key ] = ( val , time ) <EOL> def delete ( self , key ) : <EOL> res = self . _client . delete ( key , time = <NUM_LIT:0> ) <EOL> if not res : <EOL> raise KeyError ( key ) <EOL> def get ( self , key ) : <EOL> val = self . _client . get ( key ) <EOL> if val is None : <EOL> raise KeyError ( key ) <EOL> else : <EOL> return val <EOL> def clear ( self ) : <EOL> self . _client . flush_all ( ) </s>
