<s> """<STR_LIT>""" <EOL> import copy <EOL> from oslo_cache import core as cache <EOL> from oslo_config import cfg <EOL> from nova . i18n import _ <EOL> class _DeprecatedListOpt ( cfg . ListOpt ) : <EOL> def __ne__ ( self , another ) : <EOL> self_dict = copy . deepcopy ( vars ( self ) ) <EOL> another_dict = copy . deepcopy ( vars ( another ) ) <EOL> self_dict . pop ( '<STR_LIT>' ) <EOL> self_dict . pop ( '<STR_LIT>' ) <EOL> another_dict . pop ( '<STR_LIT>' ) <EOL> another_dict . pop ( '<STR_LIT>' ) <EOL> return self_dict != another_dict <EOL> memcache_opts = [ <EOL> _DeprecatedListOpt ( '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> deprecated_for_removal = True ) , <EOL> ] <EOL> CONF = cfg . CONF <EOL> CONF . register_opts ( memcache_opts ) <EOL> WEEK = <NUM_LIT> <EOL> def list_opts ( ) : <EOL> """<STR_LIT>""" <EOL> return [ ( None , copy . deepcopy ( memcache_opts ) ) ] <EOL> def get_memcached_client ( expiration_time = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if CONF . memcached_servers : <EOL> return CacheClient ( <EOL> _get_custom_cache_region ( expiration_time = expiration_time , <EOL> backend = '<STR_LIT>' , <EOL> url = CONF . memcached_servers ) ) <EOL> elif CONF . cache . enabled and CONF . cache . memcache_servers : <EOL> return CacheClient ( <EOL> _get_default_cache_region ( expiration_time = expiration_time ) ) <EOL> raise RuntimeError ( _ ( '<STR_LIT>' ) ) <EOL> def get_client ( expiration_time = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if CONF . memcached_servers : <EOL> return CacheClient ( <EOL> _get_custom_cache_region ( expiration_time = expiration_time , <EOL> backend = '<STR_LIT>' , <EOL> url = CONF . memcached_servers ) ) <EOL> elif CONF . cache . enabled : <EOL> return CacheClient ( <EOL> _get_default_cache_region ( expiration_time = expiration_time ) ) <EOL> return CacheClient ( <EOL> _get_custom_cache_region ( expiration_time = expiration_time , <EOL> backend = '<STR_LIT>' ) ) <EOL> def _get_default_cache_region ( expiration_time ) : <EOL> region = cache . create_region ( ) <EOL> if expiration_time != <NUM_LIT:0> : <EOL> CONF . cache . expiration_time = expiration_time <EOL> cache . configure_cache_region ( CONF , region ) <EOL> return region <EOL> def _get_custom_cache_region ( expiration_time = WEEK , <EOL> backend = None , <EOL> url = None ) : <EOL> """<STR_LIT>""" <EOL> region = cache . create_region ( ) <EOL> region_params = { } <EOL> if expiration_time != <NUM_LIT:0> : <EOL> region_params [ '<STR_LIT>' ] = expiration_time <EOL> if backend == '<STR_LIT>' : <EOL> region_params [ '<STR_LIT>' ] = { '<STR_LIT>' : expiration_time } <EOL> elif backend == '<STR_LIT>' : <EOL> region_params [ '<STR_LIT>' ] = { '<STR_LIT:url>' : url } <EOL> else : <EOL> raise RuntimeError ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> region . configure ( backend , ** region_params ) <EOL> return region <EOL> class CacheClient ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , region ) : <EOL> self . region = region <EOL> def get ( self , key ) : <EOL> value = self . region . get ( key ) <EOL> if value == cache . NO_VALUE : <EOL> return None <EOL> return value <EOL> def get_or_create ( self , key , creator ) : <EOL> return self . region . get_or_create ( key , creator ) <EOL> def set ( self , key , value ) : <EOL> return self . region . set ( key , value ) <EOL> def add ( self , key , value ) : <EOL> return self . region . get_or_create ( key , lambda : value ) <EOL> def delete ( self , key ) : <EOL> return self . region . delete ( key ) <EOL> def get_multi ( self , keys ) : <EOL> values = self . region . get_multi ( keys ) <EOL> return [ None if value is cache . NO_VALUE else value for value in <EOL> values ] <EOL> def delete_multi ( self , keys ) : <EOL> return self . region . delete_multi ( keys ) </s>
