Skip to content

Commit 757ccd7

Browse files
committed
Cache Django.js views and introduce settings.JS_CACHE_DURATION
1 parent b61a05e commit 757ccd7

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

djangojs/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
'JS_I18N_APPS': None,
2727
'JS_I18N_APPS_EXCLUDE': None,
2828
'JS_I18N_PATTERNS': tuple(),
29+
'JS_CACHE_DURATION': 24 * 60,
2930
'JQUERY_VERSION': JQUERY_DEFAULT_VERSION,
3031
}
3132

djangojs/tests/test_urls.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22

3+
from django.core.cache import cache
34
from django.core.urlresolvers import reverse
45
from django.test import TestCase
56
from django.test.utils import override_settings
@@ -11,6 +12,7 @@ class UrlsTestMixin(object):
1112

1213
def setUp(self):
1314
self.result = self.get_result()
15+
cache.clear()
1416

1517
def test_simple_url(self):
1618
'''It should serialize a simple URL without parameters'''

djangojs/views.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@
99
import re
1010

1111
from django.http import HttpResponse
12+
from django.utils.cache import patch_vary_headers
13+
from django.views.decorators.cache import cache_page
1214
from django.views.generic import View, TemplateView
1315

1416
from djangojs.conf import settings
1517
from djangojs.urls_serializer import urls_as_dict, urls_as_json
1618
from djangojs.utils import StorageGlobber, LazyJsonEncoder, class_from_string
1719

20+
1821
logger = logging.getLogger(__name__)
1922

2023

@@ -37,7 +40,21 @@
3740
JAVASCRIPT_MIMETYPE = 'application/javascript'
3841

3942

40-
class JsInitView(TemplateView):
43+
class CacheMixin(object):
44+
'''Apply a JS_CACHE_DURATION to the view'''
45+
def dispatch(self, *args, **kwargs):
46+
cache = cache_page(60 * settings.JS_CACHE_DURATION)
47+
return cache(super(CacheMixin, self).dispatch)(*args, **kwargs)
48+
49+
50+
class UserCacheMixin(CacheMixin):
51+
def dispatch(self, *args, **kwargs):
52+
response = super(UserCacheMixin, self).dispatch(*args, **kwargs)
53+
patch_vary_headers(response, ('Cookie',))
54+
return response
55+
56+
57+
class JsInitView(UserCacheMixin, TemplateView):
4158
'''
4259
Render a javascript file containing the URLs mapping and the context as JSON.
4360
'''
@@ -67,15 +84,15 @@ def get(self, request, **kwargs):
6784
)
6885

6986

70-
class UrlsJsonView(JsonView):
87+
class UrlsJsonView(CacheMixin, JsonView):
7188
'''
7289
Render the URLs as a JSON object.
7390
'''
7491
def get_context_data(self, **kwargs):
7592
return urls_as_dict()
7693

7794

78-
class ContextJsonView(JsonView):
95+
class ContextJsonView(UserCacheMixin, JsonView):
7996
'''
8097
Render the context as a JSON object.
8198
'''

doc/settings.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,17 @@ pattern
190190
)
191191
192192
193+
Cache management
194+
~~~~~~~~~~~~~~~~
195+
196+
``JS_CACHE_DURATION``
197+
---------------------
198+
199+
**Default**: ``24 * 60`` (24 hours)
200+
201+
Django.js urls and context cache duration in minutes.
202+
203+
193204
Usage exemple
194205
-------------
195206

0 commit comments

Comments
 (0)