Skip to content
This repository was archived by the owner on Dec 20, 2022. It is now read-only.

Commit 97724fe

Browse files
committed
Improve anonymous page cache
1 parent adbc681 commit 97724fe

File tree

1 file changed

+9
-19
lines changed

1 file changed

+9
-19
lines changed

correctiv_justizgelder/urls.py

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,21 @@
1+
from functools import wraps
2+
13
from django.conf.urls import patterns, url
24
from django.utils.translation import ugettext_lazy as _
3-
from django.utils.decorators import decorator_from_middleware_with_args
4-
from django.middleware.cache import CacheMiddleware
5+
from django.views.decorators.cache import cache_page
56

67
from .views import OrganisationSearchView, OrganisationDetail
78

89
CACHE_TIME = 15 * 60
910

1011

11-
def cache_page_anonymous(*args, **kwargs):
12-
"""
13-
Decorator to cache Django views only for anonymous users.
14-
Use just like the decorator cache_page:
15-
16-
@cache_page_anonymous(60 * 30) # cache for 30 mins
17-
def your_view_here(request):
18-
...
19-
"""
20-
key_prefix = kwargs.pop('key_prefix', None)
21-
return decorator_from_middleware_with_args(CacheMiddleware)(
22-
cache_timeout=args[0],
23-
key_prefix=key_prefix,
24-
cache_anonymous_only=True)
25-
26-
2712
def c(view):
28-
return cache_page_anonymous(CACHE_TIME)(view)
13+
@wraps(view)
14+
def cache_page_anonymous(request, *args, **kwargs):
15+
if request.user.is_authenticated():
16+
return view(request, *args, **kwargs)
17+
return cache_page(CACHE_TIME)(view)(request, *args, **kwargs)
18+
return cache_page_anonymous
2919

3020

3121
urlpatterns = patterns('',

0 commit comments

Comments
 (0)