Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

If cache_time is set to 0, don't even try caching. Closing #7

  • Loading branch information...
commit 499977a9c5f3debe130acc0d986cf58707b70fdb 1 parent 5f74ce7
@zerok authored
View
28 flatblocks/templatetags/flatblock_tags.py
@@ -17,7 +17,9 @@
The number of seconds that text should get cached after it has been
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::
@@ -46,8 +48,11 @@
from flatblocks import settings
+import logging
+
register = template.Library()
+logger = logging.getLogger(__name__)
FlatBlock = models.get_model('flatblocks', 'flatblock')
@@ -96,7 +101,8 @@ def prepare(self, parser, token):
self.tpl_is_variable = True
else:
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):
self.prepare(parser, token)
@@ -142,8 +148,10 @@ def render(self, context):
new_ctx = template.Context({})
new_ctx.update(context)
try:
- cache_key = settings.CACHE_PREFIX + real_slug
- flatblock = cache.get(cache_key)
+ flatblock = None
+ if self.cache_time != 0:
+ cache_key = settings.CACHE_PREFIX + real_slug
+ flatblock = cache.get(cache_key)
if flatblock is None:
# if flatblock's slug is hard-coded in template then it is
@@ -157,7 +165,17 @@ def render(self, context):
slug=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:
tmpl = loader.get_template(real_template)
View
5 test_project/settings.py
@@ -1,5 +1,8 @@
import os
import sys
+import logging
+
+logging.basicConfig(level=logging.DEBUG)
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
#sys.path.insert(0, os.path.dirname(PROJECT_ROOT))
@@ -15,7 +18,7 @@
'django.contrib.sessions',
'flatblocks',
)
-LANGUAGE_CODE="no"
+LANGUAGE_CODE="en"
TEMPLATE_DIRS = (
os.path.join(PROJECT_ROOT, 'templates'),
)
View
12 test_project/templates/index.html
@@ -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>
View
2  test_project/urls.py
@@ -1,10 +1,12 @@
from django.conf.urls.defaults import *
from django.contrib.admin.views.decorators import staff_member_required
from django.contrib import admin
+from . import views
admin.autodiscover()
urlpatterns = patterns('',
url('^flatblocks/', include("flatblocks.urls")),
url('^admin/', include(admin.site.urls)),
+ url('^/?', views.index),
)
View
5 test_project/views.py
@@ -0,0 +1,5 @@
+from django.shortcuts import render_to_response
+
+
+def index(request):
+ return render_to_response('index.html')
Please sign in to comment.
Something went wrong with that request. Please try again.