<s> """<STR_LIT>""" <EOL> from oslo . config import cfg <EOL> from rack . openstack . common import timeutils <EOL> memcache_opts = [ <EOL> cfg . ListOpt ( '<STR_LIT>' , <EOL> default = None , <EOL> help = '<STR_LIT>' ) , <EOL> ] <EOL> CONF = cfg . CONF <EOL> CONF . register_opts ( memcache_opts ) <EOL> def get_client ( memcached_servers = None ) : <EOL> client_cls = Client <EOL> if not memcached_servers : <EOL> memcached_servers = CONF . memcached_servers <EOL> if memcached_servers : <EOL> try : <EOL> import memcache <EOL> client_cls = memcache . Client <EOL> except ImportError : <EOL> pass <EOL> return client_cls ( memcached_servers , debug = <NUM_LIT:0> ) <EOL> class Client ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . cache = { } <EOL> def get ( self , key ) : <EOL> """<STR_LIT>""" <EOL> now = timeutils . utcnow_ts ( ) <EOL> for k in self . cache . keys ( ) : <EOL> ( timeout , _value ) = self . cache [ k ] <EOL> if timeout and now >= timeout : <EOL> del self . cache [ k ] <EOL> return self . cache . get ( key , ( <NUM_LIT:0> , None ) ) [ <NUM_LIT:1> ] <EOL> def set ( self , key , value , time = <NUM_LIT:0> , min_compress_len = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> timeout = <NUM_LIT:0> <EOL> if time != <NUM_LIT:0> : <EOL> timeout = timeutils . utcnow_ts ( ) + time <EOL> self . cache [ key ] = ( timeout , value ) <EOL> return True <EOL> def add ( self , key , value , time = <NUM_LIT:0> , min_compress_len = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if self . get ( key ) is not None : <EOL> return False <EOL> return self . set ( key , value , time , min_compress_len ) <EOL> def incr ( self , key , delta = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> value = self . get ( key ) <EOL> if value is None : <EOL> return None <EOL> new_value = int ( value ) + delta <EOL> self . cache [ key ] = ( self . cache [ key ] [ <NUM_LIT:0> ] , str ( new_value ) ) <EOL> return new_value <EOL> def delete ( self , key , time = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if key in self . cache : <EOL> del self . cache [ key ] </s>
