Return 404 for unsupported languages in ssi_included views,
[django-ssify.git] / ssify / cache.py
1 # -*- coding: utf-8 -*-
2 # This file is part of django-ssify, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See README.md for more information.
4 #
5 from __future__ import unicode_literals
6 from django.core.cache import InvalidCacheBackendError
7 from .conf import conf
8
9
10 DEFAULT_TIMEOUT = object()
11
12
13 try:
14     from django.core.cache import caches
15 except ImportError:
16     from django.core.cache import get_cache
17 else:
18     get_cache = lambda alias: caches[alias]
19
20
21 def get_caches():
22     try:
23         return [get_cache(c) for c in  conf.CACHE_ALIASES]
24     except:
25         try:
26             return [get_cache('ssify')]
27         except InvalidCacheBackendError:
28             return [get_cache('default')]
29
30
31 def cache_include(path, content, timeout=DEFAULT_TIMEOUT, version=None):
32     for cache in get_caches():
33         if timeout is DEFAULT_TIMEOUT:
34             cache.set(path, content, version=version)
35         else:
36             cache.set(path, content, timeout=timeout, version=version)
37
38
39 def flush_ssi_includes(paths=None):
40     for cache in get_caches():
41         if paths is None:
42             cache.clear()
43         else:
44             cache.delete_many(paths)