Skip to content
This repository has been archived by the owner on Dec 9, 2019. It is now read-only.

Commit

Permalink
If cache_time is set to 0, don't even try caching. Closing #7
Browse files Browse the repository at this point in the history
  • Loading branch information
zerok committed May 28, 2011
1 parent 5f74ce7 commit 499977a
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 6 deletions.
28 changes: 23 additions & 5 deletions flatblocks/templatetags/flatblock_tags.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
The number of seconds that text should get cached after it has been The number of seconds that text should get cached after it has been
fetched from the database. fetched from the database.
This field is option and defaults to no caching. This field is optional and defaults to no caching (0).
To use Django's default caching length use None.
Example:: Example::
Expand Down Expand Up @@ -46,8 +48,11 @@


from flatblocks import settings from flatblocks import settings


import logging



register = template.Library() register = template.Library()
logger = logging.getLogger(__name__)


FlatBlock = models.get_model('flatblocks', 'flatblock') FlatBlock = models.get_model('flatblocks', 'flatblock')


Expand Down Expand Up @@ -96,7 +101,8 @@ def prepare(self, parser, token):
self.tpl_is_variable = True self.tpl_is_variable = True
else: else:
self.tpl_name = self.tpl_name[1:-1] self.tpl_name = self.tpl_name[1:-1]
self.cache_time = int(self.cache_time) if self.cache_time is not None and self.cache_time != 'None':
self.cache_time = int(self.cache_time)


def __call__(self, parser, token): def __call__(self, parser, token):
self.prepare(parser, token) self.prepare(parser, token)
Expand Down Expand Up @@ -142,8 +148,10 @@ def render(self, context):
new_ctx = template.Context({}) new_ctx = template.Context({})
new_ctx.update(context) new_ctx.update(context)
try: try:
cache_key = settings.CACHE_PREFIX + real_slug flatblock = None
flatblock = cache.get(cache_key) if self.cache_time != 0:
cache_key = settings.CACHE_PREFIX + real_slug
flatblock = cache.get(cache_key)
if flatblock is None: if flatblock is None:


# if flatblock's slug is hard-coded in template then it is # if flatblock's slug is hard-coded in template then it is
Expand All @@ -157,7 +165,17 @@ def render(self, context):
slug=real_slug, slug=real_slug,
defaults = {'content': real_slug} defaults = {'content': real_slug}
) )
cache.set(cache_key, flatblock, int(self.cache_time)) if self.cache_time != 0:
if self.cache_time is None or self.cache_time == 'None':
logger.debug("Caching %s for the cache's default timeout"
% (real_slug,))
cache.set(cache_key, flatblock)
else:
logger.debug("Caching %s for %s seconds" % (real_slug,
str(self.cache_time)))
cache.set(cache_key, flatblock, int(self.cache_time))
else:
logger.debug("Don't cache %s" % (real_slug,))


if self.with_template: if self.with_template:
tmpl = loader.get_template(real_template) tmpl = loader.get_template(real_template)
Expand Down
5 changes: 4 additions & 1 deletion test_project/settings.py
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,8 @@
import os import os
import sys import sys
import logging

logging.basicConfig(level=logging.DEBUG)


PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__)) PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
#sys.path.insert(0, os.path.dirname(PROJECT_ROOT)) #sys.path.insert(0, os.path.dirname(PROJECT_ROOT))
Expand All @@ -15,7 +18,7 @@
'django.contrib.sessions', 'django.contrib.sessions',
'flatblocks', 'flatblocks',
) )
LANGUAGE_CODE="no" LANGUAGE_CODE="en"
TEMPLATE_DIRS = ( TEMPLATE_DIRS = (
os.path.join(PROJECT_ROOT, 'templates'), os.path.join(PROJECT_ROOT, 'templates'),
) )
Expand Down
12 changes: 12 additions & 0 deletions test_project/templates/index.html
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,12 @@
{% load flatblock_tags %}<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8" />
<title>Test page for flatblock rendering</title>
</head>
<body>
{% flatblock 'test1' 0 %} <!-- No caching -->
{% flatblock 'test2' 10 %} <!-- 10s caching -->
{% flatblock 'test3' None %} <!-- Default length caching -->
</body>
</html>
2 changes: 2 additions & 0 deletions test_project/urls.py
Original file line number Original file line Diff line number Diff line change
@@ -1,10 +1,12 @@
from django.conf.urls.defaults import * from django.conf.urls.defaults import *
from django.contrib.admin.views.decorators import staff_member_required from django.contrib.admin.views.decorators import staff_member_required
from django.contrib import admin from django.contrib import admin
from . import views


admin.autodiscover() admin.autodiscover()


urlpatterns = patterns('', urlpatterns = patterns('',
url('^flatblocks/', include("flatblocks.urls")), url('^flatblocks/', include("flatblocks.urls")),
url('^admin/', include(admin.site.urls)), url('^admin/', include(admin.site.urls)),
url('^/?', views.index),
) )
5 changes: 5 additions & 0 deletions test_project/views.py
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.shortcuts import render_to_response


def index(request):
return render_to_response('index.html')

0 comments on commit 499977a

Please sign in to comment.