diff --git a/geonode/proxy/utils.py b/geonode/proxy/utils.py index 96187f33b0c..800ab9001e0 100644 --- a/geonode/proxy/utils.py +++ b/geonode/proxy/utils.py @@ -2,13 +2,17 @@ from django.conf import settings from django.db.models import signals -from django.core.cache import caches site_url = urlsplit(settings.SITEURL) PROXIED_LINK_TYPES = ["OGC:WMS", "OGC:WFS", "data"] -proxy_allowed_hosts_cache = caches["proxy_allowed_hosts"] +if not settings.MEMCACHED_ENABLED: + from django.core.cache import caches + + proxy_cache = caches["proxy_allowed_hosts"] +else: + from django.core.cache import cache as proxy_cache class ProxyUrlsRegistry: @@ -19,13 +23,11 @@ def initialize(self): from geonode.geoserver.helpers import ogc_server_settings # since we are initializing the cache, we will clear the previous values - cache_val = proxy_allowed_hosts_cache.get("proxy_list") - if cache_val: - proxy_allowed_hosts_cache.set("proxy_list", {}) + + if not proxy_cache.get("proxy_list") or proxy_cache.get("proxy_list"): + proxy_cache.set("proxy_list", {}) # adding the value of the proxy in - proxy_allowed_hosts_cache.set( - "proxy_list", set([site_url.hostname] + list(getattr(settings, "PROXY_ALLOWED_HOSTS", ()))) - ) + proxy_cache.set("proxy_list", set([site_url.hostname] + list(getattr(settings, "PROXY_ALLOWED_HOSTS", ())))) if ogc_server_settings: self.register_host(ogc_server_settings.hostname) @@ -40,26 +42,27 @@ def initialize(self): self._first_init = False def set(self, hosts): - proxy_allowed_hosts_cache.set("proxy_list", set(hosts)) + proxy_cache.set("proxy_list", set(hosts)) return self def clear(self): - proxy_allowed_hosts_cache.get("proxy_list").clear() + if proxy_cache.get("proxy_list"): + proxy_cache.set("proxy_list", {}) return self def register_host(self, host): - prev = proxy_allowed_hosts_cache.get("proxy_list") + prev = proxy_cache.get("proxy_list") if prev: old_hosts = prev.copy() old_hosts.add(host) host = old_hosts - proxy_allowed_hosts_cache.set("proxy_list", host) + proxy_cache.set("proxy_list", host) def unregister_host(self, host): - proxy_allowed_hosts_cache.get("proxy_list").remove(host) + proxy_cache.get("proxy_list").remove(host) def get_proxy_allowed_hosts(self): - return proxy_allowed_hosts_cache.get("proxy_list") + return proxy_cache.get("proxy_list") proxy_urls_registry = ProxyUrlsRegistry() diff --git a/geonode/settings.py b/geonode/settings.py index 8dbebea3c70..cc80c28c007 100644 --- a/geonode/settings.py +++ b/geonode/settings.py @@ -369,10 +369,6 @@ "TIMEOUT": 600, "OPTIONS": {"MAX_ENTRIES": 10000}, }, - "proxy_allowed_hosts": { - "BACKEND": "django.core.cache.backends.filebased.FileBasedCache", - "LOCATION": os.getenv("FILE_SYSTEM_CACHE_LOCATION", "/tmp/django_cache"), - }, } # Whitenoise Settings - ref.: http://whitenoise.evans.io/en/stable/django.html @@ -390,6 +386,11 @@ "BACKEND": MEMCACHED_BACKEND, "LOCATION": MEMCACHED_LOCATION, } +else: + CACHES["proxy_allowed_hosts"] = { + "BACKEND": "django.core.cache.backends.filebased.FileBasedCache", + "LOCATION": os.getenv("FILE_SYSTEM_CACHE_LOCATION", "/tmp/django_cache"), + } # Define the STATICFILES_STORAGE accordingly if not DEBUG and CACHE_BUSTING_STATIC_ENABLED: