【问题标题】:Django CACHEOPS different timeout for different querysetsDjango CACHEOPS 不同查询集的不同超时
【发布时间】:2021-05-10 16:52:13
【问题描述】:

我正在使用 Django CACHEOPS。 cacheops' README

在 settings.py 中如何为不同的查询集配置不同的超时时间?

(缓存 get 查询集 10 秒,缓存 queryset fetches 60 秒)

类似这样的:(这显然有重复密钥错误)

CACHEOPS = {
    'blog.Article': {'ops': 'fetch', 'timeout': 60},
    'blog.Article': {'ops': 'get', 'timeout': 10},
}

我的目标是:我想缓存每篇文章详细页面比文章列表页面更长的时间。

【问题讨论】:

    标签: django caching redis django-redis


    【解决方案1】:

    如您所见,CACHEOPS 定义中的一个键是针对模型的,而不是针对查询集的

    blog.ArticleArticle 应用程序中的Article 模型, 不是查询集。

    那么知道上面你只有解决方法来解决 cacheops 中的故障,它会是

    CACHEOPS_DEFAULTS = {
        'timeout': 60         # first level - a default
    }
    
    CACHEOPS = {
        'blog.*': {'ops': 'fetch', 'timeout': 60},  # second level - a default per application
        'blog.Article': {'ops': 'get', 'timeout': 10},
    }
    

    而是使用装饰器来覆盖特定模型上的特定get

    from cacheops import cached_as
    @cached_as(Article, timeout=10)
    def get_cached_article(...
         Article.objects. ... your queryset
         ...
    

    【讨论】:

      猜你喜欢
      • 2022-01-23
      • 2012-01-04
      • 1970-01-01
      • 1970-01-01
      • 2011-03-19
      • 2015-08-15
      • 1970-01-01
      • 2023-03-25
      • 2014-11-07
      相关资源
      最近更新 更多