Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #6195 -- Documented caching options for javascript_catalog.

  • Loading branch information...
commit 0836670c5cd8bb17322504c46e07d3944add63c3 1 parent 5d88358
Aymeric Augustin authored February 25, 2013
2  docs/topics/conditional-view-processing.txt
@@ -27,7 +27,7 @@ instead of a full response, telling the client that nothing has changed.
27 27
 When you need more fine-grained control you may use per-view conditional
28 28
 processing functions.
29 29
 
30  
-.. conditional-decorators:
  30
+.. _conditional-decorators:
31 31
 
32 32
 The ``condition`` decorator
33 33
 ===========================
46  docs/topics/i18n/translation.txt
@@ -946,6 +946,52 @@ This isn't as fast as string interpolation in Python, so keep it to those
946 946
 cases where you really need it (for example, in conjunction with ``ngettext``
947 947
 to produce proper pluralizations).
948 948
 
  949
+Note on performance
  950
+-------------------
  951
+
  952
+The :func:`~django.views.i18n.javascript_catalog` view generates the catalog
  953
+from ``.mo`` files on every request. Since its output is constant — at least
  954
+for a given version of a site — it's a good candidate for caching.
  955
+
  956
+Server-side caching will reduce CPU load. It's easily implemented with the
  957
+:func:`~django.views.decorators.cache.cache_page` decorator. To trigger cache
  958
+invalidation when your translations change, provide a version-dependant key
  959
+prefix, as shown in the example below, or map the view at a version-dependant
  960
+URL.
  961
+
  962
+.. code-block:: python
  963
+
  964
+    from django.views.decorators.cache import cache_page
  965
+    from django.views.i18n import javascript_catalog
  966
+
  967
+    # The value returned by get_version() must change when translations change.
  968
+    @cache_page(86400, key_prefix='js18n-%s' % get_version())
  969
+    def cached_javascript_catalog(request, domain='djangojs', packages=None):
  970
+        return javascript_catalog(request, domain, packages)
  971
+
  972
+Client-side caching will save bandwidth and make your site load faster. If
  973
+you're using ETags (:setting:`USE_ETAGS = True <USE_ETAGS>`), you're already
  974
+covered. Otherwise, you can apply :ref:`conditional decorators
  975
+<conditional-decorators>`. In the following example, the cache is invalidated
  976
+whenever your restart your application server.
  977
+
  978
+.. code-block:: python
  979
+
  980
+    from django.utils import timezone
  981
+    from django.views.decorators.http import last_modified
  982
+    from django.views.i18n import javascript_catalog
  983
+
  984
+    last_modified_date = timezone.now()
  985
+    @last_modified(lambda req, **kw: last_modified_date)
  986
+    def cached_javascript_catalog(request, domain='djangojs', packages=None):
  987
+        return javascript_catalog(request, domain, packages)
  988
+
  989
+You can even pre-generate the javascript catalog as part of your deployment
  990
+procedure and serve it as a static file. This radical technique is implemented
  991
+in django-statici18n_.
  992
+
  993
+.. _django-statici18n: http://django-statici18n.readthedocs.org/en/latest/
  994
+
949 995
 .. _url-internationalization:
950 996
 
951 997
 Internationalization: in URL patterns

0 notes on commit 0836670

Please sign in to comment.
Something went wrong with that request. Please try again.