Cache a django view that has URL parameters

2019-02-03 10:55发布

I have recently implemented Django's excellent cache framework. However from what I understand Django will not cache a view that is passed parameters in a get request. I have an Ajax view that is passed get parameters that I would like to cache for X seconds, what would be an easy way to do this?

In psuedo code I currently have a URL:

http://mysites/ajaxthing/?user=foo&items=10

I would like to cache any this url as long as it has the same get parameters.

I'm currently using the cache decorators in my view:

myview(stuff)

myview = cache_page(myview, 60 * 3)

I did read about django's vary headers but it went a little over my head, and I'm not even sure its the correct solution

5条回答
Evening l夕情丶
2楼-- · 2019-02-03 11:23

This should be no longer an issue in Django 1.3+. See: https://docs.djangoproject.com/en/dev/topics/cache/#using-vary-headers

查看更多
来,给爷笑一个
3楼-- · 2019-02-03 11:32

It appears that you no longer need to do anything more complicated than placing @cache_page([length of time]) above your View function you are trying to cache, irrespective of whether you have parameters in the URL.

For example, if you have a url like:

http://example.com/user/some_user_id

Your view function in views.py would look something like this:

from django.views.decorators.cache import cache_page
...

@cache_page(60 * 10)
def get_user_detail(request, user_id=None):
    ...
    return render(...)
查看更多
做自己的国王
4楼-- · 2019-02-03 11:36

a bit late, but you can use django-view-cache-utils for that.

查看更多
Emotional °昔
5楼-- · 2019-02-03 11:41

Right, vary headers is not the correct solution, it's used when you want to cache based on client request headers like user-agent etc.

You'll need to use low-level API or template fragment caching. It depends on your views really.

With low-level API it looks something like this:

from django.core.cache import cache

def get_user(request):
    user_id = request.GET.get("user_id")
    user = cache.get("user_id_%s"%user_id)
    if user is None:
        user = User.objects.get(pk=user_id)
        cache.set("user_id_%s"%user_id, user, 10*60) # 10 minutes
    ...
    ..
    .
查看更多
聊天终结者
6楼-- · 2019-02-03 11:47

Yes, you can use django-view-cache-utils, here is code for your case:

from view_cache_utils import cache_page_with_prefix
from django.utils.hashcompat import md5_constructor
...
@cache_page_with_prefix(60*15, lambda request: md5_constructor(request.get_full_path()).hexdigest())
def my_view(request):
    ...
查看更多
登录 后发表回答