Django 中的全局变量/变量缓存

发布于 2024-12-20 02:53:48 字数 627 浏览 6 评论 0原文

在我的网站中,我想在多个页面的侧边栏中向用户展示浏览次数最多的产品类别。

因此,在每个不同的视图中,我有:

variables = RequestContext(request, {
   (...)
   'most_viewed_cats': calculate_most_viewed_categories()
}

在各种模板

{% include "list_most_viewed_categories" %}

和那个模板中:

<ul>
{% for cat in most_viewed_cats %}
    {{ cat.name }}
{% empty %}
    </ul>No categories to show.<ul>
{% endfor %} 
</ul>

但是,我想每 2 天左右只计算一次most_viewed_categories 值,而不是在每个视图中计算它。

我知道视图可以被缓存,但这更多的是变量缓存。是否可以将此变量缓存在 Django 服务器中的某个位置,并仅在该时间段后更新它?人们会如何去做这件事呢?

谢谢

In my website, I want to present to the user the most viewed product categories in a sidebar, in multiple pages.

so in each different view I have:

variables = RequestContext(request, {
   (...)
   'most_viewed_cats': calculate_most_viewed_categories()
}

and in the various templates

{% include "list_most_viewed_categories" %}

and in that one:

<ul>
{% for cat in most_viewed_cats %}
    {{ cat.name }}
{% empty %}
    </ul>No categories to show.<ul>
{% endfor %} 
</ul>

However, I would like to calculate the most_viewed_categories value only once every 2 days or so, instead of calculating it in every view.

I know views can be cached, but this is more of a variable caching. Is it possible to cache this variable somewhere in the Django server and renew it only after that period of time? How would one go about doing this?

Thank you

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

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

发布评论

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

评论(1

つ可否回来 2024-12-27 02:53:48

如果您想在整个站点上重用它,您可能最好创建一个上下文处理器,该处理器始终使用包含查看次数最多的类别的变量填充模板上下文

from django.core.cache import get_cache

CACHE_KEY_MOST_VIEWED_CATEGORIES = 'most_viewed_categories'
cache = get_cache('default')

def _get_most_viewed_categories():
    raise NotImplemented("Haven't figured out how to collect the value and cache it")

def most_viewed_categories(request):
    '''Provides the most viewed categories for every template context.'''

    return {
        'most_viewed_categories': cache.get(CACHE_KEY_MOST_VIEWED_CATEGORIES, 
                                            _get_most_viewed_categories)
    }

但无论如何,为了缓存以下值数据库,您需要获取缓存并使用 cache.setcache.get 来存储和检索值。

If it's something you'd like to reuse across the whole site, you would probably do best to create a context processor that's going to always populate the template context with the variable containing the most viewed categories

from django.core.cache import get_cache

CACHE_KEY_MOST_VIEWED_CATEGORIES = 'most_viewed_categories'
cache = get_cache('default')

def _get_most_viewed_categories():
    raise NotImplemented("Haven't figured out how to collect the value and cache it")

def most_viewed_categories(request):
    '''Provides the most viewed categories for every template context.'''

    return {
        'most_viewed_categories': cache.get(CACHE_KEY_MOST_VIEWED_CATEGORIES, 
                                            _get_most_viewed_categories)
    }

But in any case, for caching a value in the database, you'll need to obtain the cache and use cache.set and cache.get to store and retrieve the value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文