【问题标题】:How to force webapp2_cached_property to re-evaluate如何强制 webapp2 cached_property 重新评估
【发布时间】:2015-09-22 23:20:50
【问题描述】:

我知道webapp2_cached_property 在第一次调用之后用数据替换一个方法,然后每次调用它,因此我的问题出现了。

我有一个多语言网站,我正在使用一个表单来构建一些简单的选择菜单。选择菜单是改变语言的数据。显然,如果用户更改系统上的语言,我想重建表单。但是,我不想删除webapp2_cached_property,因为它会在每次用户调用相同的 url 时重建表单,这会减慢系统速度。那么有谁知道强制webapp2_cached_property按需重新评估的方法,例如当客户更改语言时。目前,除了默认语言的选择数据外,我还有所选语言的所有其他内容。任何肮脏和肮脏的东西都可以!啊,是的,这只发生在生产上,而不是在开发服务器上......

class HomeRequestHandler(BaseHandler):
    """
    Handler to show the home page
    """

    def get(self):
        params = {}
        params['products'] = []
        params['max_searches'] = 1
        params['user_search_count'] = 0

       return self.render_template('index.html', **params)

    @webapp2.cached_property
    def form(self):
        import product.product_data.forms as forms
        return forms.ProductForm(self)

好的,我尝试了以下方法,但生产中的语言仍然没有改变......

将此添加到我的基本处理程序中 - 它正在工作!

if hasattr(self, 'form'):
        if self.locale != self.oldLocale and hasattr(self, 'form_no_cache'):
            new_form = self.form_no_cache
            kwargs['form'] = new_form()
            logging.info('reset form')
            logging.info(kwargs['form'].product_type())
        else:
            kwargs['form'] = self.form
            logging.info('using cached form')

并将其添加到我的家庭处理程序中

def form_no_cache(self):
    import product.product_data.forms as forms
    return forms.ProductForm(self)

所有这些在开发中都很好,并且日志在开发和生产中似乎是正确的......

大家有什么想法吗?

【问题讨论】:

  • 谢谢,帮了大忙!不知道为什么我的谷歌搜索没有出现,一定是迟到了......
  • 查看上面的最新更新,仍然有同样的问题,但在开发服务器中,logging.info(kwargs['form'].product_type()) 显示正确的语言,但在生产中没有。 ..

标签: python forms google-app-engine caching webapp2


【解决方案1】:

好的,无法弄清楚这一点,因此重写了代码以在简单的 def 中构建选择并作为参数而不是表单传递。似乎正在工作。虽然我对 wtforms 有怀疑,但不知道很痛苦,但没有时间......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-14
    • 2012-08-19
    • 1970-01-01
    相关资源
    最近更新 更多