Created
December 13, 2011 15:05
-
-
Save aaugustin/1472445 to your computer and use it in GitHub Desktop.
Cache Django's javascript_catalog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
fairly old but does its job