基于Django视图的缓存与authed用户

时间:2022-07-14 19:38:21

So I am using view based cache in django as I have some views that are not really suitable for caching and others that really are. However, in some of these views that I cache the output will be different for different users. Is there a way to have a view based cache separate for different users? The @vary_on_cookie decorator looked like exactly what I needed but it doesn't seem to work for view based cache's?

所以我在django中使用基于视图的缓存,因为我有一些不适合缓存的视图和其他真正适合的视图。但是,在某些视图中,我缓存输出对于不同的用户将是不同的。有没有办法让不同用户分开基于视图的缓存? @vary_on_cookie装饰器看起来就像我需要的但它似乎不适用于基于视图的缓存?

At the moment around my view I have:

在我的观点周围,我有:

@vary_on_cookie
@cache_page(60 * 5)
def view(request):

If you log in as anonmymous you can see what was cache's by a logged in user.

如果您以anonmymous身份登录,则可以通过登录用户查看缓存的内容。

Any ideas? I know I could probably use the low level cache for this type of problem but I'm surprised if there wasn't an easier django way of doing it, seems like it would be a common problem.

有任何想法吗?我知道我可能会使用低级缓存来解决这类问题,但如果没有更简单的django方法,我会感到惊讶,这似乎是一个常见的问题。

Thanks

谢谢

Tom

汤姆

2 个解决方案

#1


0  

You may simply use client-based caching with cache_control. Like:

您可以简单地使用基于客户端的缓存和cache_control。喜欢:

@cache_control(max_age=60 * 5)
def view(request):

#2


0  

There's a snippet here that uses the Django cache framework. I think you can modify it for yourself. There's line 38:

这里有一个使用Django缓存框架的片段。我想你可以自己修改它。第38行:

key = make_cache_key(request.get_full_path(), getattr(request, 'supports_html5', None))

You can change it to

您可以将其更改为

key = make_cache_key(request.user[.id], request.get_full_path(), getattr(request, 'supports_html5', None))

So that users have different page keys.

这样用户就有了不同的页面键。

#1


0  

You may simply use client-based caching with cache_control. Like:

您可以简单地使用基于客户端的缓存和cache_control。喜欢:

@cache_control(max_age=60 * 5)
def view(request):

#2


0  

There's a snippet here that uses the Django cache framework. I think you can modify it for yourself. There's line 38:

这里有一个使用Django缓存框架的片段。我想你可以自己修改它。第38行:

key = make_cache_key(request.get_full_path(), getattr(request, 'supports_html5', None))

You can change it to

您可以将其更改为

key = make_cache_key(request.user[.id], request.get_full_path(), getattr(request, 'supports_html5', None))

So that users have different page keys.

这样用户就有了不同的页面键。