缓存日历日期的 Django 查询集
我有一个查询,结果每天只改变一次。似乎对我收到的该页面的每个请求都执行该查询是一种浪费。我正在研究使用 memcached 来实现这一点。
我该如何开始呢?有人对我在使用 Django 缓存时应该避免的建议或陷阱有什么建议吗?我应该在模板还是视图上缓存?
这个问题可能看起来很模糊,但这只是因为我以前从未处理过缓存。因此,如果有什么我可以详细说明的,请请提出。
Ken Cochrane 的详细说明
:
此数据更改的频率:相关数据将锁定在该日历日期。因此,例如,我将提取 2011 年 1 月 30 日的数据,并且我可以在 2011 年 1 月 31 日刷新该数据之前全天提供该缓存副本。
我是否在多个地方使用此数据: 仅在一个视图中。
数据量是多少:平均 10 个模型对象,包含大约 15 个字段,最大的是
CharField(max_length=120)
。我将使用values()
将字段数量减少到大约一半。
I have a query which results only change once a day. Seems like a waste to be performing that query every request I get for that page. I am investigating using memcached for this.
How would I begin? Anyone have any suggestions or pitfalls I should avoid in using Django's caching? Should I cache at the template or at the view?
This question might seem vague but it's only because I've never dealt with caching before. So if there's something I could elaborate on, please just ask.
Elaboration
Per Ken Cochrane:
How often does this data change: The relevant data would be locked in for that calendar date. So, for example, I'll pull the data for 1/30/2011 and I'm okay with serving that cached copy for the whole day until 1/31/2011 where it would be refreshed.
Do I use this data in more then one place: Only in one view.
How much data is it going to be: An average of 10 model objects that contain about 15 fields with the largest being a
CharField(max_length=120)
. I will trim the number of fields down usingvalues()
to about half of those.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通常,在决定在哪里进行缓存之前,我会问自己几个问题。
由于我不知道您的应用程序的所有详细信息,我将做出一些假设。
有了这些假设,你就有 3 个选择。
1. 缓存模板
2. 缓存视图
3. 缓存查询集
通常,当我进行缓存时,我会缓存查询集,这使我能够更好地控制如何缓存数据,并且我可以在多个地方重用相同的缓存数据。
我发现缓存查询集的最简单方法是在 ModelManger 中为相关模型执行此操作。我会创建一个像 get_calender_by_date(date) 这样的方法来为我处理查询和缓存。这是一个粗略的模型
缓存时需要注意的一些事项
缓存模板或视图时,您需要注意以下
希望这能为您提供足够的信息来开始。祝你好运。
Normally before I decide where to do the caching I ask myself a few questions.
Since I don't know all of the details for your application, I'm going to make some assumptions.
With these assumptions you have 3 options.
1. cache the templates
2. cache the view
3. cache the queryset
Normally when I do my caching I cache the queryset, this allows me greater control of how I want to cache the data and I can reuse the same cached data in more then one place.
The easiest way that I have found to cache the queryset is to do this in the ModelManger for the model in question. I would create a method like get_calender_by_date(date) that will handle the query and caching for me. Here is a rough mockup
Some things to look out for when caching
When caching the template or view you need to watch out for the following
Hopefully that gives you enough information to get started. Good Luck.
首先尝试阅读此内容。
Django 有能力{% 缓存 for_seconds 某物 %}
只需使用缓存标签即可。
http://docs.djangoproject.com/en/dev/topics/cache/
Try to read this first of all.
Django has an ability to {% cache for_seconds something %}
Just use cache tag.
http://docs.djangoproject.com/en/dev/topics/cache/
您可以使用 Python 的内置
lru_cache
按日期缓存函数的结果,只要方法参数是普通的“2021-09-22”日期而不是时间戳即可:You can cache the results of a function by date with Python's builtin
lru_cache
, as long as the method param is a plain "2021-09-22" date and not a timestamp: