Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added caching of filer urls using the filer logic path as cache key. …

…Invalidate cache when referenced resource is being saved.

Completely removed filercss.
  • Loading branch information...
commit a95da350b453c03de0b066ebf1bf4d98c3b6256d 1 parent 023ec5c
@kux kux authored
View
17 filertags/signals.py
@@ -2,11 +2,14 @@
import re
import urlparse
+from django.core.cache import cache
from django.db.models import signals
+from django.template.defaultfilters import slugify
from filer.models.filemodels import File
from filer.models.imagemodels import Image
-from templatetags.filertags import filerfile
+
+from templatetags.filertags import filerfile, get_filerfile_cache_key
_LOGICAL_URL_TEMPLATE = "/* logicalurl('%s') */"
@@ -138,5 +141,17 @@ def resolve_css_resource_urls(instance, **kwargs):
_update_referencing_css_files(instance)
+def clear_urls_cache(instance, **kwargs):
+ """Clears urls cached by the filerfile tag. """
+ logical_file_path = os.path.join(
+ _construct_logical_folder_path(instance),
+ instance.original_filename)
+ cache_key = get_filerfile_cache_key(logical_file_path)
+ cache.delete(cache_key)
+
+
signals.post_save.connect(resolve_css_resource_urls, sender=File)
signals.post_save.connect(resolve_css_resource_urls, sender=Image)
+
+signals.post_save.connect(clear_urls_cache, sender=File)
+signals.post_save.connect(clear_urls_cache, sender=Image)
View
40 filertags/templatetags/filertags.py
@@ -1,7 +1,8 @@
from django import template
-from django.template.defaultfilters import stringfilter
from django.db.models import Q
-from django.core.urlresolvers import reverse
+from django.core.cache import cache
+from django.template.defaultfilters import stringfilter, slugify
+
from filer.models import File, Folder
@@ -28,22 +29,41 @@ def filerthumbnail(path):
pass
+def get_filerfile_cache_key(path):
+ return 'filer-%s' % slugify(path)
+
+
+import logging
+logger = logging.getLogger(__name__)
+
+
def filerfile(path):
- ft = filerthumbnail(path)
- if ft is None or not ft:
- return ''
- return ft.url
+ """django-filer has two concepts of paths:
+ * the logical path: media/images/foobar.png
+ * the actual url: filer_public/2012/11/22/foobar.png
+ This tag returns the actual url associated with the logical path.
+
+ Since most of the templates will be referencing the same
+ resources (css, js), the returned urls are being cached.
+ """
+ cache_key = get_filerfile_cache_key(path)
+ if cache.has_key(cache_key):
+ return cache.get(cache_key)
+ file_obj = filerthumbnail(path)
+ if file_obj is None or not file_obj:
+ url = ''
+ else:
+ url = file_obj.url
+ cache.set(cache_key, url)
+ return url
+
def mustache(path):
url = filerfile(path)
return 'http://mustachify.me/?src=%s' % url
-def filercss(path):
- return reverse('css-preprocessor', args=[path])
-
register = template.Library()
register.filter(stringfilter(filerthumbnail))
register.filter(stringfilter(filerfile))
-register.filter(stringfilter(filercss))
register.filter(stringfilter(mustache))
View
7 filertags/urls.py
@@ -1,7 +0,0 @@
-from django.conf.urls.defaults import url, patterns
-from views import css_preprocessor
-
-
-urlpatterns = patterns('',
- url(r'^(.+)$', css_preprocessor, name='css-preprocessor')
-)
View
25 filertags/views.py
@@ -1,25 +0,0 @@
-# Create your views here.
-import re
-import os.path
-
-from django.http import HttpResponse, Http404
-from templatetags.filertags import filerthumbnail, filerfile
-
-def css_preprocessor(request, path):
- dirname = os.path.dirname(path)
- f = filerthumbnail(path)
- if not f and path.endswith('.css'):
- raise Http404('No such file.')
- local_cache = {}
-
- def change_urls(match):
- relative_path = match.groups()[0]
- path = os.path.normpath(os.path.join(dirname, relative_path))
- if not path in local_cache:
- local_cache[path] = "url('%s')" % filerfile(path)
- return local_cache[path]
-
-
- return HttpResponse(re.sub(
- r"url\(['\"]([^'\"]+)['\"]\)", change_urls, f.read()
- ), mimetype='text/css')
Please sign in to comment.
Something went wrong with that request. Please try again.