mirror of
https://github.com/jazzband/django-dbtemplates.git
synced 2026-04-27 10:14:55 +00:00
Merge 231a1ca73f into 29cedc5271
This commit is contained in:
commit
fad9fa243b
3 changed files with 43 additions and 3 deletions
3
CONTRIBUTORS
Normal file
3
CONTRIBUTORS
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
Author: Stephan Peijnik <spe@anexia.at>
|
||||
ANEXIA Internetdienstleistungs GmbH
|
||||
http://www.anexia.at/
|
||||
|
|
@ -4,6 +4,7 @@ from django.template import TemplateDoesNotExist
|
|||
from dbtemplates.conf import settings
|
||||
from dbtemplates.models import Template
|
||||
from dbtemplates.utils.cache import cache, get_cache_key, set_and_return
|
||||
from dbtemplates.utils.cache import get_cache_notfound_key
|
||||
from django.template.loader import BaseLoader
|
||||
|
||||
|
||||
|
|
@ -19,6 +20,18 @@ class Loader(BaseLoader):
|
|||
is_usable = True
|
||||
|
||||
def load_template_source(self, template_name, template_dirs=None):
|
||||
# The logic should work like this:
|
||||
# * Try to find the template in the cache. If found, return it.
|
||||
# * Now check the cache if a lookup for the given template
|
||||
# has failed lately and hand over control to the next template
|
||||
# loader waiting in line.
|
||||
# * If this still did not fail we first try to find a site-specific
|
||||
# template in the database.
|
||||
# * On a failure from our last attempt we try to load the global
|
||||
# template from the database.
|
||||
# * If all of the above steps have failed we generate a new key
|
||||
# in the cache indicating that queries failed, with the current
|
||||
# timestamp.
|
||||
site = Site.objects.get_current()
|
||||
display_name = 'dbtemplates:%s:%s:%s' % (settings.DATABASE_ENGINE,
|
||||
template_name, site.domain)
|
||||
|
|
@ -30,15 +43,32 @@ class Loader(BaseLoader):
|
|||
return backend_template, template_name
|
||||
except:
|
||||
pass
|
||||
|
||||
# Not found in cache, move on.
|
||||
cache_notfound_key = get_cache_notfound_key(template_name)
|
||||
if cache:
|
||||
try:
|
||||
notfound = cache.get(cache_notfound_key)
|
||||
if notfound:
|
||||
raise TemplateDoesNotExist(template_name)
|
||||
except:
|
||||
raise TemplateDoesNotExist(template_name)
|
||||
|
||||
# Not marked as not-found, move on...
|
||||
|
||||
try:
|
||||
template = Template.objects.get(name__exact=template_name)
|
||||
template = Template.objects.get(name__exact=template_name,
|
||||
sites__in=[site.id])
|
||||
return set_and_return(cache_key, template.content, display_name)
|
||||
except (Template.MultipleObjectsReturned, Template.DoesNotExist):
|
||||
except Template.DoesNotExist:
|
||||
try:
|
||||
template = Template.objects.get(
|
||||
name__exact=template_name, sites__in=[site.id])
|
||||
name__exact=template_name)
|
||||
return set_and_return(
|
||||
cache_key, template.content, display_name)
|
||||
except Template.DoesNotExist:
|
||||
pass
|
||||
|
||||
# Mark as not-found in cache.
|
||||
cache.set(cache_notfound_key, '1')
|
||||
raise TemplateDoesNotExist(template_name)
|
||||
|
|
|
|||
|
|
@ -15,6 +15,12 @@ def get_cache_key(name):
|
|||
current_site = Site.objects.get_current()
|
||||
return 'dbtemplates::%s::%s' % (name, current_site.pk)
|
||||
|
||||
def get_cache_notfound_key(name):
|
||||
return get_cache_key(name)+'::notfound'
|
||||
|
||||
def remove_notfound_key(instance):
|
||||
# Remove notfound key as soon as we save the template.
|
||||
cache.delete(get_cache_notfound_key(instance.name))
|
||||
|
||||
def set_and_return(cache_key, content, display_name):
|
||||
# Save in cache backend explicitly if manually deleted or invalidated
|
||||
|
|
@ -29,6 +35,7 @@ def add_template_to_cache(instance, **kwargs):
|
|||
in the database was added or changed.
|
||||
"""
|
||||
remove_cached_template(instance)
|
||||
remove_notfound_key(instance)
|
||||
cache.set(get_cache_key(instance.name), instance.content)
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue