缓存具有URL参数的django视图

时间:2020-11-30 19:38:09

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?

我最近实现了Django优秀的缓存框架。但是根据我的理解,Django不会缓存在get请求中传递参数的视图。我有一个传入的Ajax视图get参数我想要缓存X秒,有什么简单的方法呢?

In psuedo code I currently have a URL:

在psuedo代码中,我目前有一个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.

我想缓存任何这个url,只要它有相同的get参数。

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

我确实读过关于django不同的标题,但是它有点超出我的理解,我甚至不确定它是正确的解决方案

5 个解决方案

#1


16  

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.

是的,变化头不是正确的解决方案,当您希望基于客户端请求头(如user-agent等)进行缓存时,使用它。

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

您将需要使用低级API或模板片段缓存。这取决于你的观点。

With low-level API it looks something like this:

对于低级API,它看起来是这样的:

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
    ...
    ..
    .

#2


8  

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

是的,你可以使用django-view-cache-utils,这里是你的案例代码:

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):
    ...

#3


4  

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

在Django 1.3+中,这应该不再是个问题。见:https://docs.djangoproject.com/en/dev/topics/cache/ using-vary-headers

#4


2  

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.

看起来您不需要再做任何比将@cache_page([time])放在您要缓存的视图函数之上更复杂的事情,不管URL中是否有参数。

For example, if you have a url like:

例如,如果您有如下url:

http://example.com/user/some_user_id

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

视图中的视图函数。py看起来是这样的:

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

@cache_page(60 * 10)
def get_user_detail(request, user_id=None):
    ...
    return render(...)

#5


0  

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

有点晚了,但是您可以使用django-view-cache-utils。

#1


16  

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.

是的,变化头不是正确的解决方案,当您希望基于客户端请求头(如user-agent等)进行缓存时,使用它。

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

您将需要使用低级API或模板片段缓存。这取决于你的观点。

With low-level API it looks something like this:

对于低级API,它看起来是这样的:

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
    ...
    ..
    .

#2


8  

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

是的,你可以使用django-view-cache-utils,这里是你的案例代码:

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):
    ...

#3


4  

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

在Django 1.3+中,这应该不再是个问题。见:https://docs.djangoproject.com/en/dev/topics/cache/ using-vary-headers

#4


2  

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.

看起来您不需要再做任何比将@cache_page([time])放在您要缓存的视图函数之上更复杂的事情,不管URL中是否有参数。

For example, if you have a url like:

例如,如果您有如下url:

http://example.com/user/some_user_id

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

视图中的视图函数。py看起来是这样的:

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

@cache_page(60 * 10)
def get_user_detail(request, user_id=None):
    ...
    return render(...)

#5


0  

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

有点晚了,但是您可以使用django-view-cache-utils。