Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cache_page(per-page-cache) is not working correctly. #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions django_mobile/cache/__init__.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
from functools import wraps
from django.views.decorators.cache import cache_page as _cache_page
from django.utils.decorators import decorator_from_middleware
from django_mobile.cache.middleware import CacheFlavourMiddleware

from django_mobile.cache.middleware import CacheFlavourMiddleware, CacheFlavourRequestMiddleware, \
CacheFlavourResponseMiddleware

__all__ = ('cache_page', 'vary_on_flavour')

__all__ = ('cache_page', 'vary_on_flavour')

vary_on_flavour = decorator_from_middleware(CacheFlavourMiddleware)
vary_on_flavour_request = decorator_from_middleware(CacheFlavourRequestMiddleware)
vary_on_flavour_response = decorator_from_middleware(CacheFlavourResponseMiddleware)


def cache_page(*args, **kwargs):
'''
Same as django's ``cache_page`` decorator, but wraps the view into
``vary_on_flavour`` decorator before. Makes it possible to serve multiple
flavours without getting into trouble with django's caching that doesn't
``vary_on_flavour_request`` and ``vary_on_flavour_response`` decorators.
Makes it possible to serve multiple flavours without getting into trouble with django's caching that doesn't
know about flavours.
'''
decorator = _cache_page(*args, **kwargs)

def flavoured_decorator(func):
return decorator(vary_on_flavour(func))
return vary_on_flavour_request(_cache_page(*args, **kwargs)(vary_on_flavour_response(func)))

return flavoured_decorator
8 changes: 7 additions & 1 deletion django_mobile/cache/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
from django.utils.cache import patch_vary_headers


class CacheFlavourMiddleware(object):
class CacheFlavourRequestMiddleware(object):
def process_request(self, request):
_set_request_header(request, get_flavour(request))


class CacheFlavourResponseMiddleware(object):
def process_response(self, request, response):
patch_vary_headers(response, ['X-Flavour'])
return response


class CacheFlavourMiddleware(CacheFlavourRequestMiddleware, CacheFlavourResponseMiddleware):
pass