Skip to content

Commit 706eff7

Browse files
Merge pull request #64 from ojomio/master
Fixed bug in `cache_page` decorator, splitting middleware in two
2 parents 12585ad + 8c7a595 commit 706eff7

3 files changed

Lines changed: 38 additions & 20 deletions

File tree

README.rst

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,11 @@ You can also use django's caching middlewares
152152
``django.middleware.cache.UpdateCacheMiddleware`` and
153153
``FetchFromCacheMiddleware`` like you already do. But to make them aware of
154154
flavours, you need to add
155-
``django_mobile.cache.middleware.CacheFlavourMiddleware`` as second last item
156-
in the ``MIDDLEWARE_CLASSES`` settings, right before
157-
``FetchFromCacheMiddleware``.
155+
``django_mobile.cache.middleware.FetchFromCacheFlavourMiddleware`` item before standard Django ``FetchFromCacheMiddleware``
156+
in the ``MIDDLEWARE_CLASSES`` settings and ``django_mobile.cache.middleware.UpdateCacheFlavourMiddleware`` before
157+
``django_mobile.cache.middleware.UpdateCacheMiddleware`` correspondingly.
158158

159+
It is necessary to split the usage of ``CacheMiddleware`` because some additional work should be done on request and response *before* standard caching behavior and that is not possible while using two complete middlewares in either order
159160

160161
Reference
161162
=========
@@ -194,16 +195,19 @@ Reference
194195
to ``DEFAULT_MOBILE_FLAVOUR`` settings value in case.
195196

196197
``django_mobile.cache.cache_page``
197-
Same as django's ``cache_page`` decorator but applies ``vary_on_flavour``
198-
before the view is decorated with
199-
``django.views.decorators.cache.cache_page``.
198+
Same as django's ``cache_page`` decorator, but wraps the view into
199+
additional decorators before and after that. Makes it possible to serve multiple
200+
flavours without getting into trouble with django's caching that doesn't
201+
know about flavours.
200202

201-
``django_mobile.cache.vary_on_flavour``
202-
A decorator created from the ``CacheFlavourMiddleware`` middleware.
203+
``django_mobile.cache.vary_on_flavour_fetch`` ``django_mobile.cache.vary_on_flavour_update``
204+
Decorators created from the ``FetchFromCacheFlavourMiddleware`` and ``UpdateCacheFlavourMiddleware`` middleware.
203205

204-
``django_mobile.cache.middleware.CacheFlavourMiddleware``
205-
Adds ``X-Flavour`` header to ``request.META`` in ``process_request`` and
206-
adds this header to ``response['Vary']`` in ``process_response``.
206+
``django_mobile.cache.middleware.FetchFromCacheFlavourMiddleware``
207+
Adds ``X-Flavour`` header to ``request.META`` in ``process_request``
208+
209+
``django_mobile.cache.middleware.UpdateCacheFlavourMiddleware``
210+
Adds ``X-Flavour`` header to ``response['Vary']`` in ``process_response`` so that Django's ``CacheMiddleware`` know that it should take into account the content of this header when looking up the cached content on next request to this URL.
207211

208212

209213
Customization

django_mobile/cache/__init__.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
from functools import wraps
2-
from django.views.decorators.cache import cache_page as _cache_page
3-
from django.utils.decorators import decorator_from_middleware
4-
from django_mobile.cache.middleware import CacheFlavourMiddleware
52

3+
from django.views.decorators.cache import cache_page as _django_cache_page
4+
from django.utils.decorators import decorator_from_middleware
5+
from django_mobile.cache.middleware import FetchFromCacheFlavourMiddleware, UpdateCacheFlavourMiddleware
66

7-
__all__ = ('cache_page', 'vary_on_flavour')
7+
__all__ = ('cache_page', 'vary_on_flavour_fetch', 'vary_on_flavour_update')
88

99

10-
vary_on_flavour = decorator_from_middleware(CacheFlavourMiddleware)
10+
vary_on_flavour_fetch = decorator_from_middleware(FetchFromCacheFlavourMiddleware)
11+
vary_on_flavour_update = decorator_from_middleware(UpdateCacheFlavourMiddleware)
1112

1213

1314
def cache_page(*args, **kwargs):
1415
'''
1516
Same as django's ``cache_page`` decorator, but wraps the view into
16-
``vary_on_flavour`` decorator before. Makes it possible to serve multiple
17+
additional decorators before and after that. Makes it possible to serve multiple
1718
flavours without getting into trouble with django's caching that doesn't
1819
know about flavours.
1920
'''
20-
decorator = _cache_page(*args, **kwargs)
21+
decorator = _django_cache_page(*args, **kwargs)
2122
def flavoured_decorator(func):
22-
return decorator(vary_on_flavour(func))
23+
return vary_on_flavour_fetch(decorator(vary_on_flavour_update(func)))
2324
return flavoured_decorator

django_mobile/cache/middleware.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
1-
from django_mobile import get_flavour, _set_request_header
1+
import warnings
2+
23
from django.utils.cache import patch_vary_headers
4+
from django_mobile import get_flavour, _set_request_header
35

46

57
class CacheFlavourMiddleware(object):
8+
def __init__(self):
9+
warnings.warn('CacheFlavourMiddleware does nothing and should be abandoned.'
10+
'The intended behavior cannot be implemented using one middleware.'
11+
'Use separate FetchFromCacheFlavourMiddleware and UpdateCacheFlavourMiddleware instead.'
12+
'Refer to https://github.com/gregmuellegger/django-mobile/pull/64 for details',
13+
category=DeprecationWarning)
14+
15+
16+
class FetchFromCacheFlavourMiddleware(object):
617
def process_request(self, request):
718
_set_request_header(request, get_flavour(request))
819

20+
21+
class UpdateCacheFlavourMiddleware(object):
922
def process_response(self, request, response):
1023
patch_vary_headers(response, ['X-Flavour'])
1124
return response

0 commit comments

Comments
 (0)