CherryPy缓存如何工作? [英] How does CherryPy caching work?

查看:149
本文介绍了CherryPy缓存如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近发现了CherryPy中的页面对象属性在请求之间(以及客户端之间)持续存在。所以我想知道,将页面输出存储在这种属性中有意义吗?像这样:

  class Page:
def default(self,pagenumber):
如果pagenumber不在self.validpages:
return'找不到页码'
尝试:
html = self.pageoutput [pagenumber]
除了KeyError:
html = self.formatter( self.dbcall(pagenumber))
return html
default.exposed = True

def formatter(self,data):
html =#格式化代码
return html
$ b $ def dbcall(self,pagenumber):
data =#数据库查找代码
返回数据

我知道CherryPy 默认缓存GET请求。在我的测试中,当一个对象属性是输出的一部分并且该属性发生变化时,CherryPy提供了该属性的新值。这是否意味着输出仅被部分缓存?



对我来说,只要每次更改数据库时更新self.pageoutput,这都会很有用。我能想象的唯一困难是如果我想显示用户特定的信息。你怎么看?

解决方案

CherryPy默认不缓存GET请求;您必须按照该文档中的描述明确打开缓存工具。



要回答您的第一个问题,是的,存储诸如pageoutput通话间不会改变。但是,有几点需要注意:


  1. HTTP缓存远远好于您自己写的。因此,请优先选择整个响应。

  2. 因此,对部分响应使用临时缓存,例如模板和横幅等。非常小心地设计您的存储安全并发。请参阅有关该主题的 effbot's writeup 开始。通常,如果可行的话,尝试在应用程序启动时生成和存储这些值,而不是在请求期间;如果仅在启动时在主线程中写入此类数据,则每个请求应该可以安全地由多个线程读取。如果您需要这些数据随应用程序状态的改变而改变,那么您可能希望使用一个数据库或其他一些有数百年工作时间的机制来使其安全并发。

  3. ol>

    I recently discovered that page object attributes in CherryPy are persistent between requests (and between clients). So I'm wondering, would it make sense to store page output in such an attribute? Like this:

    class Page:
        def default(self, pagenumber):
            if pagenumber not in self.validpages:
                return 'Page number not found'
            try:
                html = self.pageoutput[pagenumber]
            except KeyError:
                html = self.formatter(self.dbcall(pagenumber))
            return html
        default.exposed = True
    
        def formatter(self, data):
            html = # Formatting code here
            return html
    
        def dbcall(self, pagenumber):
            data = # Database lookup code here
            return data
    

    I know CherryPy caches GET requests by default. In my tests, when an object attribute was part of the output and that attribute changed, CherryPy served the attribute's new value. Does that mean the output was only partially cached?

    To me this would be useful as long as you updated self.pageoutput every time you changed your database. The only difficulty I could imagine is if I wanted to display user-specific information. What do you think?

    解决方案

    CherryPy does not cache GET requests by default; you have to explicitly turn on the caching tool as described in that documentation.

    To answer your first question, yes, it's perfectly valid to store things like "pageoutput" that do not change between calls. However, there are a couple of caveats:

    1. HTTP caching is far better than what you can write on your own. So prefer that for whole responses.
    2. Therefore, use ad-hoc caching for parts of responses, such as templates and banners and such.
    3. Be very careful to design your storage to be safely concurrent. See effbot's writeup on that subject for a start. In general, try to generate and store such values at application startup if feasible, instead of during a request; if you write such data in the main thread only at startup, it should be safely readable by multiple threads for each request. If you need such data to change as the application state progresses, you probably want to use a database or some other mechanism that has had hundreds of man-years of work to make it safely concurrent.

    这篇关于CherryPy缓存如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆