Skip to content

Instantly share code, notes, and snippets.

@aaugustin
Created December 13, 2011 15:05
Show Gist options
  • Save aaugustin/1472445 to your computer and use it in GitHub Desktop.
Save aaugustin/1472445 to your computer and use it in GitHub Desktop.
Cache Django's javascript_catalog
import datetime
import hashlib
from django.views.decorators.cache import cache_page
from django.views.decorators.http import condition
from django.views.i18n import javascript_catalog
def get_version():
# Write your own! That depends on your deployment strategy.
# This example won't work if you release more than once a day.
return datetime.date.today().isoformat()
# The javascript_catalog view may be cached server-side without limits,
# provided the cache is flushed when autoslave is upgraded and restarted.
key_prefix = 'js18n-%s' % get_version()
last_modified = datetime.datetime.now()
etag = hashlib.md5(key_prefix).hexdigest()
@condition(etag_func=lambda req, **kw: etag,
last_modified_func=lambda req, **kw: last_modified)
@cache_page(86400, key_prefix=key_prefix)
def cached_javascript_catalog(request, domain='djangojs', packages=None):
return javascript_catalog(request, domain, packages)
@patroqueeet
Copy link

fairly old but does its job

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment