缓存日历日期的 Django 查询集

发布于 2024-10-14 10:55:00 字数 607 浏览 7 评论 0原文

我有一个查询,结果每天只改变一次。似乎对我收到的该页面的每个请求都执行该查询是一种浪费。我正在研究使用 memcached 来实现这一点。

我该如何开始呢?有人对我在使用 Django 缓存时应该避免的建议或陷阱有什么建议吗?我应该在模板还是视图上缓存?

这个问题可能看起来很模糊,但这只是因为我以前从未处理过缓存。因此,如果有什么我可以详细说明的,请提出。

Ken Cochrane 的详细说明

  1. 此数据更改的频率:相关数据将锁定在该日历日期。因此,例如,我将提取 2011 年 1 月 30 日的数据,并且我可以在 2011 年 1 月 31 日刷新该数据之前全天提供该缓存副本。

  2. 我是否在多个地方使用此数据: 仅在一个视图中。

  3. 数据量是多少:平均 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:

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

  2. Do I use this data in more then one place: Only in one view.

  3. 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 using values() to about half of those.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷途知返 2024-10-21 10:55:00

通常,在决定在哪里进行缓存之前,我会问自己几个问题。

  1. 该数据多久更改一次
  2. 我是否在多个地方使用该数据
  3. 它将有多少数据

由于我不知道您的应用程序的所有详细信息,我将做出一些假设。

  1. 您有一个视图,它要么接受日期,要么使用当前日期来查询数据库以提取该日期的所有日历事件。
  2. 您只在一个模板上显示此信息,
  3. 数据量不太大(少于100条)。

有了这些假设,你就有 3 个选择。
1. 缓存模板
2. 缓存视图
3. 缓存查询集

通常,当我进行缓存时,我会缓存查询集,这使我能够更好地控制如何缓存数据,并且我可以在多个地方重用相同的缓存数据。

我发现缓存查询集的最简单方法是在 ModelManger 中为相关模型执行此操作。我会创建一个像 get_calender_by_date(date) 这样的方法来为我处理查询和缓存。这是一个粗略的模型

CACHE_TIMEOUT_SECONDS = 60 * 60 * 24 # this is 24 hours

class CalendarManager(models.Manager):

    def get_calendar_by_date(self, by_date):
        """ assuming date is a datetime object """
        date_key = by_date.strftime("%m_%d_%Y")
        cache_key = 'CAL_DATE_%s' % (date_key)
        cal_date = cache.get(cache_key)
        if cal_date is not None:
            return cal_date

        # not in cache get from database
        cal_date = self.filter(event_date=by_date)

        # set cal_date in cache for later use
        cache.set(cache_key, cal_date, CACHE_TIMEOUT_SECONDS)
        return cal_date

缓存时需要注意的一些事项

  1. 确保您存储在缓存中的对象可以被腌制
  2. 因为memcache不知道今天是哪一天,您需要确保不会过度缓存。例如,如果是 1 月 21 日中午,并且您缓存了 24 小时,则该日历信息将显示到 1 月 22 日中午,而这可能不是您要查找的内容,因此请确保在设置查询时间时要么将其设置为一个较小的值,以便它更快地过期,要么计算缓存多长时间,以便它在您希望它过期时过期。
  3. 确保您知道要缓存的对象的大小。如果您的 memcache 实例只有 16MB 的存储空间,但您想要存储 32MB 的数据,那么缓存不会给您带来多大好处。

缓存模板或视图时,您需要注意以下

  1. 设置缓存超时,以便它不会太大,我认为您不能以编程方式更改模板缓存超时,并且它是硬编码的,所以如果您设置得太高,您最终会得到一个过时的页面。您应该能够以编程方式更改缓存时间,因此更安全一些。
  2. 如果您正在缓存模板,并且模板上还有其他动态信息并且一直在变化,请确保仅将缓存标记放在要缓存一段时间的页面部分周围。如果你把它放在错误的地方,你可能会得到错误的结果。

希望这能为您提供足够的信息来开始。祝你好运。

Normally before I decide where to do the caching I ask myself a few questions.

  1. How often does this data change
  2. Do I use this data in more then one place
  3. How much data is it going to be

Since I don't know all of the details for your application, I'm going to make some assumptions.

  1. you have a view that either takes in a date or uses the current date to query the database to pull out all of the calender events for that date.
  2. you only display this information on one template,
  3. The amount of data isn't too large (less then 100 entries).

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

CACHE_TIMEOUT_SECONDS = 60 * 60 * 24 # this is 24 hours

class CalendarManager(models.Manager):

    def get_calendar_by_date(self, by_date):
        """ assuming date is a datetime object """
        date_key = by_date.strftime("%m_%d_%Y")
        cache_key = 'CAL_DATE_%s' % (date_key)
        cal_date = cache.get(cache_key)
        if cal_date is not None:
            return cal_date

        # not in cache get from database
        cal_date = self.filter(event_date=by_date)

        # set cal_date in cache for later use
        cache.set(cache_key, cal_date, CACHE_TIMEOUT_SECONDS)
        return cal_date

Some things to look out for when caching

  1. Make sure the objects that you are storing in the cache can be pickled
  2. Since memcache doesn't know what day it is you need to make sure you don't over cache. For example if it was Noon on Jan 21st and you cache for 24 hours, that calendar information will show up until Noon on Jan 22nd and that might not be what you are looking for, so make sure when you set the time of the query you either set it to a small value so it expires quicker or you calculate how long to cache so that it expires when you want it to expire.
  3. Make sure you know the size of the objects you want to cache. If your memcache instance only have 16MB of storage but you want to store 32MB of data, the cache isn't going to do you much good.

When caching the template or view you need to watch out for the following

  1. set your cache timeout so that it isn't too large, I don't think you can programtically change the template cache timeout, and it is hard coded, so if you set it too high you will end up having a page that is out of date. You should be able to programaticly change the cache time, so it is a little safer.
  2. If you are caching the template and there is other information on the template that is dynamic and changes all of the time, make sure that you only put the cache tags around the section of the page you want cached for a while. If you put it in the wrong place you might end up the wrong result.

Hopefully that gives you enough information to get started. Good Luck.

一页 2024-10-21 10:55:00

首先尝试阅读此内容。
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/

高冷爸爸 2024-10-21 10:55:00

您可以使用 Python 的内置 lru_cache 按日期缓存函数的结果,只要方法参数是普通的“2021-09-22”日期而不是时间戳即可:

import datetime
from functools import lru_cache

@lru_cache(maxsize=1)
def my_func(date: datetime.date):
    if type(date) is not datetime.date:
        raise ValueError(f"This method is cached by calendar date, but received date {date}.")

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:

import datetime
from functools import lru_cache

@lru_cache(maxsize=1)
def my_func(date: datetime.date):
    if type(date) is not datetime.date:
        raise ValueError(f"This method is cached by calendar date, but received date {date}.")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文