Return 404 for unsupported languages in ssi_included views,
[django-ssify.git] / ssify / cache.py
index 7f42145..699f45f 100644 (file)
@@ -1,46 +1,44 @@
-import os
-from django.core.cache.backends.filebased import FileBasedCache
-
-
-class StaticFileBasedCache(FileBasedCache):
-    def __init__(self, *args, **kwargs):
-        super(StaticFileBasedCache, self).__init__(*args, **kwargs)
-        self._dir = os.path.abspath(self._dir)
-
-    def make_key(self, key, version=None):
-        assert version is None, \
-            'StaticFileBasedCache does not support versioning.'
-        return key
-
-    def _key_to_file(self, key):
-        key = os.path.abspath(os.path.join(self._dir, key.lstrip('/')))
-        assert key.startswith(self._dir), 'Trying to save path outside root.'
-        if key.endswith('/'):
-            key += 'index.html'
-        return key
-
-    def get(self, key, default=None, version=None):
-        key = self.make_key(key, version=version)
-        self.validate_key(key)
-        fname = self._key_to_file(key)
-        try:
-            with open(fname, 'rb') as inf:
-                return inf.read()
-        except (IOError, OSError):
-            pass
-        return default
-
-    def set(self, key, value, timeout=None, version=None):
-        assert timeout is None, \
-            'StaticFileBasedCache does not support timeouts.'
-        key = self.make_key(key, version=version)
-        self.validate_key(key)
-        fname = self._key_to_file(key)
-        dirname = os.path.dirname(fname)
+# -*- coding: utf-8 -*-
+# This file is part of django-ssify, licensed under GNU Affero GPLv3 or later.
+# Copyright © Fundacja Nowoczesna Polska. See README.md for more information.
+#
+from __future__ import unicode_literals
+from django.core.cache import InvalidCacheBackendError
+from .conf import conf
+
+
+DEFAULT_TIMEOUT = object()
+
+
+try:
+    from django.core.cache import caches
+except ImportError:
+    from django.core.cache import get_cache
+else:
+    get_cache = lambda alias: caches[alias]
+
+
+def get_caches():
+    try:
+        return [get_cache(c) for c in  conf.CACHE_ALIASES]
+    except:
         try:
         try:
-            if not os.path.exists(dirname):
-                os.makedirs(dirname)
-            with open(fname, 'wb') as outf:
-                outf.write(value)
-        except (IOError, OSError):
-            pass
+            return [get_cache('ssify')]
+        except InvalidCacheBackendError:
+            return [get_cache('default')]
+
+
+def cache_include(path, content, timeout=DEFAULT_TIMEOUT, version=None):
+    for cache in get_caches():
+        if timeout is DEFAULT_TIMEOUT:
+            cache.set(path, content, version=version)
+        else:
+            cache.set(path, content, timeout=timeout, version=version)
+
+
+def flush_ssi_includes(paths=None):
+    for cache in get_caches():
+        if paths is None:
+            cache.clear()
+        else:
+            cache.delete_many(paths)