def __init__(self, *args, **kwargs): """Patch ``kwargs['cache_args']``.""" if not 'cache_args' in kwargs: # Read the ``cache.*`` ones into ``cache_args``. cache_args = {} prefix = 'mako.cache_args.' for key in settings.keys(): if key.startswith(prefix): name = key.split(prefix)[1].strip() value = settings[key] try: value = value.strip() except AttributeError: pass cache_args[name] = value coerce_cache_params(cache_args) cache_args['timeout'] = cache_args.get('expire') type_arg = cache_args.get('type') if type_arg and type_arg.startswith('dogpile.cache'): cache_args = coerge_dogpile_args(cache_args) register_plugin("dogpile.cache", "pyramid_weblayer.patch", "UnicodeKeyCapableMakoPlugin") kwargs['cache_impl'] = 'dogpile.cache' kwargs['cache_args'] = cache_args super(TemplateLookup, self).__init__(*args, **kwargs)
def __init__(self, app, config=None, environ_key='beaker.cache', **kwargs): """Initialize the Cache Middleware The Cache middleware will make a Cache instance available every request under the ``environ['beaker.cache']`` key by default. The location in environ can be changed by setting ``environ_key``. ``config`` dict All settings should be prefixed by 'cache.'. This method of passing variables is intended for Paste and other setups that accumulate multiple component settings in a single dictionary. If config contains *no cache. prefixed args*, then *all* of the config options will be used to intialize the Cache objects. ``environ_key`` Location where the Cache instance will keyed in the WSGI environ ``**kwargs`` All keyword arguments are assumed to be cache settings and will override any settings found in ``config`` """ self.app = app config = config or {} # Load up the default params self.options = dict(type='memory', data_dir=None, timeout=None, log_file=None) # Pull out any config args starting with beaker cache. if there are any for dct in [config, kwargs]: for key, val in dct.iteritems(): if key.startswith('beaker.cache.'): self.options[key[13:]] = val if key.startswith('cache.'): self.options[key[6:]] = val if key.startswith('cache_'): warnings.warn( 'Cache options should start with cache. ' 'instead of cache_', DeprecationWarning, 2) self.options[key[6:]] = val # Coerce and validate cache params coerce_cache_params(self.options) # Assume all keys are intended for cache if none are prefixed with # 'cache.' if not self.options and config: self.options = config self.options.update(kwargs) self.cache_manager = CacheManager(**self.options) self.environ_key = environ_key
def __init__(self, app, config=None, environ_key='beaker.cache', **kwargs): """Initialize the Cache Middleware The Cache middleware will make a Cache instance available every request under the ``environ['beaker.cache']`` key by default. The location in environ can be changed by setting ``environ_key``. ``config`` dict All settings should be prefixed by 'cache.'. This method of passing variables is intended for Paste and other setups that accumulate multiple component settings in a single dictionary. If config contains *no cache. prefixed args*, then *all* of the config options will be used to intialize the Cache objects. ``environ_key`` Location where the Cache instance will keyed in the WSGI environ ``**kwargs`` All keyword arguments are assumed to be cache settings and will override any settings found in ``config`` """ if self.deprecated: warnings.warn('CacheMiddleware is moving to beaker.middleware in ' '0.8', DeprecationWarning, 2) self.app = app config = config or {} # Load up the default params self.options= dict(type='memory', data_dir=None, timeout=None, log_file=None) # Pull out any config args starting with beaker cache. if there are any for dct in [config, kwargs]: for key, val in dct.iteritems(): if key.startswith('beaker.cache.'): self.options[key[13:]] = val if key.startswith('cache.'): self.options[key[6:]] = val if key.startswith('cache_'): warnings.warn('Cache options should start with cache. ' 'instead of cache_', DeprecationWarning, 2) self.options[key[6:]] = val # Coerce and validate cache params coerce_cache_params(self.options) # Assume all keys are intended for cache if none are prefixed with 'cache.' if not self.options and config: self.options = config self.options.update(kwargs) self.cache_manager = CacheManager(**self.options) self.environ_key = environ_key
def set_cache_regions_from_settings(settings): """ Add cache support to the Pylons application. The ``settings`` passed to the configurator are used to setup the cache options. Cache options in the settings should start with either 'beaker.cache.' or 'cache.'. """ cache_settings = {'regions': None} for key in settings.keys(): for prefix in ['beaker.cache.', 'cache.']: if key.startswith(prefix): name = key.split(prefix)[1].strip() cache_settings[name] = settings[key].strip() coerce_cache_params(cache_settings) if 'enabled' not in cache_settings: cache_settings['enabled'] = True regions = cache_settings['regions'] if regions: for region in regions: if not region: continue region_settings = { 'data_dir': cache_settings.get('data_dir'), 'lock_dir': cache_settings.get('lock_dir'), 'expire': cache_settings.get('expire', 60), 'enabled': cache_settings['enabled'], 'key_length': cache_settings.get('key_length', 250), 'type': cache_settings.get('type'), 'url': cache_settings.get('url'), } region_prefix = '%s.' % region region_len = len(region_prefix) for key in list(cache_settings.keys()): if key.startswith(region_prefix): region_settings[key[region_len:]] = cache_settings.pop(key) coerce_cache_params(region_settings) cache.cache_regions[region] = region_settings
def set_cache_regions_from_settings(settings): """ Add cache support to the Pylons application. The ``settings`` passed to the configurator are used to setup the cache options. Cache options in the settings should start with either 'beaker.cache.' or 'cache.'. """ cache_settings = {"regions": None} for key in settings.keys(): for prefix in ["beaker.cache.", "cache."]: if key.startswith(prefix): name = key.split(prefix)[1].strip() cache_settings[name] = settings[key].strip() coerce_cache_params(cache_settings) if "enabled" not in cache_settings: cache_settings["enabled"] = True regions = cache_settings["regions"] if regions: for region in regions: if not region: continue region_settings = { "data_dir": cache_settings.get("data_dir"), "lock_dir": cache_settings.get("lock_dir"), "expire": cache_settings.get("expire", 60), "enabled": cache_settings["enabled"], "key_length": cache_settings.get("key_length", 250), "type": cache_settings.get("type"), "url": cache_settings.get("url"), } region_prefix = "%s." % region region_len = len(region_prefix) for key in list(cache_settings.keys()): if key.startswith(region_prefix): region_settings[key[region_len:]] = cache_settings.pop(key) coerce_cache_params(region_settings) cache.cache_regions[region] = region_settings