【问题标题】:Implementing Sqlalchemy beaker caching in pyramid framework在金字塔框架中实现 Sqlalchemy 烧杯缓存
【发布时间】:2012-09-18 14:24:24
【问题描述】:

根据 sqlalchemy 文档提供的缓存 sqlalchemy 查询的示例,我们应该这样做

from caching_query import FromCache

# load Person objects.  cache the result under the namespace "all_people".
print "loading people...."
people = Session.query(Person).options(FromCache("default", "all_people")).all()

我在 development.ini 中有以下烧杯配置

cache.regions = day, hour, minute, second
cache.type = file
cache.data_dir = %(here)s/cache/sess/data
cache.lock_dir = %(here)s/cache/sess/lock
cache.second.expire = 1
cache.minute.expire = 60
cache.hour.expire = 3600
cache.day.expire = 86400

当我在我的应用程序中使用上面的示例代码时,数据没有缓存在缓存文件夹中,所以我假设基于内存的缓存是默认设置,是否可以将 sqlalchemy 缓存类型切换为基于文件的缓存?还是我弄错了?

【问题讨论】:

    标签: python caching sqlalchemy pyramid beaker


    【解决方案1】:

    您的问题缺少一些细节,但让我试试:

    • 传递给FromCache() 的第一个参数是一个Beaker 缓存区域的名称,它应该匹配其中一个配置的区域,这里不是这样。或者您可能在代码中配置default 区域(如果区域未知,我希望BeakerException 被抛出)?

    • 您需要在 Pyramid 的项目配置中安装并包含 pyramid_beaker 模块。我建议你关注pyramid_beakermanual's Setup section

    • 您需要在应用程序的 __init__.py 中添加一些额外代码,以便将 .ini 文件设置传播到 Beaker。这在手册的Beaker cache region support 部分进行了描述。

    这是我当前项目的一个工作示例,配置了基于 Beaker 的会话和缓存(所有不相关的部分都已删除):

    from pyramid.config import Configurator
    from pyramid_beaker import set_cache_regions_from_settings
    from pyramid_beaker import session_factory_from_settings
    
    def main(global_config, **settings):
        # Configure Beaker caching/sessions    
        set_cache_regions_from_settings(settings)
        session_factory = session_factory_from_settings(settings)
    
        config = Configurator(settings=settings)
        config.set_session_factory(session_factory)
        config.include('pyramid_beaker')
    
        config.add_static_view('static', 'static', cache_max_age=3600)
        config.add_route('home', '/')
    
        config.scan()
        return config.make_wsgi_app()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-29
      • 2013-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-23
      • 2016-09-18
      相关资源
      最近更新 更多