当前位置:  开发笔记 > 编程语言 > 正文

SQLAlchemy是否支持缓存?

如何解决《SQLAlchemy是否支持缓存?》经验,为你挑选了3个好方法。

SQLAlchemy是否支持某种缓存,所以如果我重复运行相同的查询,它会从缓存中返回响应而不是查询数据库?更新数据库时是否自动清除此缓存?

或者在CherryPy + SQLAlchemy设置上实现此功能的最佳方法是什么?



1> zzzeek..:

我们有一个非常全面的缓存解决方案,作为嵌入式挂钩的示例,在0.6中.这是一个子类Query的配方,使其了解Beaker,并允许通过查询选项控制显式查询的查询缓存以及延迟加载器.

我现在正在制作它.示例本身位于dist中,介绍文档位于http://www.sqlalchemy.org/docs/orm/examples.html#beaker-caching.

更新:Beaker现已被dogpile缓存取代:http://docs.sqlalchemy.org/en/latest/orm/examples.html#module-examples.dogpile_caching



2> torial..:

不是第二个问题的答案,但从此链接中的注释表明SQLAlchemy不支持缓存:http://spyced.blogspot.com/2007/01/why-sqlalchemy-impresses-me.html

雷文说......

Does SQLAlchemy do any kind of internal caching?

For example, if you ask for the same data twice (or an obvious subset
of the initially requested data) will the database be hit once or twice?

I recently wrote a caching database abstraction layer for an
application and (while fun) it was a fair bit of work to get it to a
minimally functional state. If SQLAlchemy did that I would seriously
consider jumping on the bandwagon.

I've found things in the docs that imply something like this might be
going on, but nothing explicit.
4:36 PM

乔纳森埃利斯说......

No; the author of SA [rightly, IMO] considers caching a separate concern.

What you saw in the docs is probably the SA identity map, which makes it so 
if you load an instance in  two different places, they will refer
to the same object. But the database will still be queried twice, so it is
not a cache in the sense you mean.


此链接http://www.mail-archive.com/sqlalchemy@googlegroups.com/msg15667.html建议/显示后续查询不使用查询但从身份地图返回实例,但这仅适用于您正在使用主键查询.

3> Jeff Widman..:

SQLAlchemy支持两种类型的缓存:

    缓存结果集,以便重复运行相同的查询,而不是数据库.它采用dogpile支持许多不同的后端,包括memcached,redis,和基本的平面文件.

    文档在这里:http://docs.sqlalchemy.org/en/latest/orm/examples.html#module-examples.dogpile_caching

    缓存query对象,以便Python解释器不必每次都手动重新组装查询字符串.调用这些查询并调用baked queries缓存baked.基本上它会缓存sqlalchemy在访问数据库之前所需的所有操作- 它不会减少数据库调用.最初的基准测试显示,query在代码冗长度略有增加的折衷下,代代可以加速高达40%.

    文档在这里:http://docs.sqlalchemy.org/en/latest/orm/extensions/baked.html

推荐阅读
农大军乐团_697
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有