【问题标题】:what's differece between the django's cache ?the The per-site cache,The per-view cache,Specifying per-view cache and Template fragment cachingdjango的缓存有什么区别?每个站点缓存,每个视图缓存,指定每个视图缓存和模板片段缓存
【发布时间】:2011-02-19 12:54:56
【问题描述】:

我知道 django 有一些缓存方法,例如每个站点缓存、每个视图缓存、指定每个视图缓存和模板片段缓存 但是这些缓存之间有什么区别? 每个站点缓存意味着缓存系统缓存整个站点? 我如何理解“缓存整个网站”这句话?

【问题讨论】:

    标签: python django caching memcached


    【解决方案1】:

    我认为文档在描述这一点方面做得很好,但我会将其粘贴在这里并给出一些描述。

    缓存整个站点意味着 django 将尝试缓存您通过中间件设置的每个视图。

    每个站点的缓存文档
    http://docs.djangoproject.com/en/dev/topics/cache/#the-per-site-cache

    设置缓存后,使用缓存的最简单方法是缓存整个站点。

    基本上,它是一组中间件,因此它会缓存所有视图。中间件应用于每个请求/响应。


    按视图缓存文档
    http://docs.djangoproject.com/en/dev/topics/cache/#the-per-view-cache

    使用缓存的更细粒度的方式 框架是通过缓存的输出 个人意见。 django.views.decorators.cache 定义了一个 cache_page 装饰器,它会自动为你缓存视图的响应。

    这是按视图缓存。您可以通过将@cache_page 装饰器应用于特定视图(而不是上面的所有视图)来决定缓存某个视图


    模板片段缓存文档
    http://docs.djangoproject.com/en/dev/topics/cache/#template-fragment-caching

    {% cache %} 模板标签缓存 给定块的内容 时间。

    这使您可以缓存模板的块(而不是上面的整个视图),例如,您可以在模板中缓存昂贵的查询,而网站的其他部分仍然动态提供。

    如果某些部分无法缓存,这将很有用。例如,在顶部显示登录用户的经典问题将无法使用按视图缓存,因为用户需要更新因此缓存失效。


    如果您在文档中进一步阅读,您还将获得缓存 api:

    缓存 api 文档
    http://docs.djangoproject.com/en/dev/topics/cache/#the-low-level-cache-api

    这基本上是您在 python 代码中控制缓存的方式(与上面的模板相反)。

    # imagine you have a function that takes a day to complete.
    cache.set('very_expensive_homepage_logic', takes_a_day_to_calculate())
    
    # now if you call get() with your key, it will be returned and you won't have to wait a day to calculate.
    cache.get('very_expensive_homepage_logic')
    

    【讨论】:

      猜你喜欢
      • 2013-12-07
      • 2013-08-18
      • 2012-01-07
      • 2017-07-15
      • 2021-04-02
      • 2011-04-11
      • 1970-01-01
      • 2015-12-16
      • 1970-01-01
      相关资源
      最近更新 更多