From 0432b1a6f9a27bb9a805c9e98147b7c1aeaa032c Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Wed, 1 Apr 2020 12:10:19 -0400 Subject: [PATCH] Move default caching_config to PluginConfig class --- docs/plugins/development.md | 15 +++++++++++++-- netbox/extras/plugins/__init__.py | 6 ++++-- netbox/netbox/settings.py | 18 +++++------------- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/docs/plugins/development.md b/docs/plugins/development.md index 8a0619c54..9f9be2514 100644 --- a/docs/plugins/development.md +++ b/docs/plugins/development.md @@ -360,17 +360,28 @@ template_extensions = [AddSiteAnimal, AddRackAnimal] ## Caching Configuration -By default, all query operations within a plugin are cached. To change this, define a caching configuration under the PluginConfig class' `caching_config` attribute. An example configuration is below: +By default, all query operations within a plugin are cached. To change this, define a caching configuration under the PluginConfig class' `caching_config` attribute. All configuration keys will be applied within the context of the plugin; there is no need to include the plugin name. An example configuration is below: ```python class MyPluginConfig(PluginConfig): ... caching_config = { - 'my_plugin.foo': { + 'foo': { 'ops': 'get', 'timeout': 60 * 15, }, + '*': { + 'ops': 'all', + } } ``` +To disable caching for your plugin entirely, set: + +```python +caching_config = { + '*': None +} +``` + See the [django-cacheops](https://github.com/Suor/django-cacheops) documentation for more detail on configuring caching. diff --git a/netbox/extras/plugins/__init__.py b/netbox/extras/plugins/__init__.py index 644188823..6e515dd5b 100644 --- a/netbox/extras/plugins/__init__.py +++ b/netbox/extras/plugins/__init__.py @@ -44,8 +44,10 @@ class PluginConfig(AppConfig): # Middleware classes provided by the plugin middleware = [] - # Caching configuration - caching_config = {} + # Cacheops configuration. Cache all operations by default. + caching_config = { + '*': {'ops': 'all'}, + } # Default integration paths. Plugin authors can override these to customize the paths to # integrated components. diff --git a/netbox/netbox/settings.py b/netbox/netbox/settings.py index 370081155..c172aeaa9 100644 --- a/netbox/netbox/settings.py +++ b/netbox/netbox/settings.py @@ -689,16 +689,8 @@ for plugin_name in PLUGINS: PLUGINS_CONFIG[plugin_name][setting] = value # Apply cacheops config - plugin_cacheops = plugin_config.caching_config - if plugin_cacheops: - if type(plugin_cacheops) is not dict: - raise ImproperlyConfigured(f"Plugin {plugin_name} caching_config must be a dictionary.") - for key in plugin_cacheops.keys(): - # Validate config is only being set for the given plugin - app = key.split('.')[0] - if app != plugin_name: - raise ImproperlyConfigured(f"Plugin {plugin_name} may not modify caching config for another app: {app}") - else: - # Apply the default config like all other core apps - plugin_cacheops = {f"{plugin_name}.*": {'ops': 'all'}} - CACHEOPS.update(plugin_cacheops) + if type(plugin_config.caching_config) is not dict: + raise ImproperlyConfigured(f"Plugin {plugin_name} caching_config must be a dictionary.") + CACHEOPS.update({ + f"{plugin_name}.{key}": value for key, value in plugin_config.caching_config.items() + })