質問

SQLAlchemyは何らかのキャッシュをサポートしていますか?同じクエリを繰り返し実行すると、データベースをクエリする代わりにキャッシュからの応答を返しますか? DBが更新されると、このキャッシュは自動的にクリアされますか?

または、CherryPy + SQLAlchemyセットアップでこれを実装する最良の方法は何ですか?

役に立ちましたか?

解決

0.6には、埋め込みフックと組み合わせた例として、非常に包括的なキャッシュソリューションがあります。クエリをサブクラス化し、ビーカーを認識させ、明示的なクエリとクエリオプションを介したレイジーローダーのクエリキャッシングを制御できるようにするためのレシピです。

現在、本番環境で実行しています。サンプル自体はdistにあり、イントロのドキュメントは http://にあります。 www.sqlalchemy.org/docs/orm/examples.html#beaker-caching

更新:ビーカーは dogpile キャッシングに置き換えられました: http://docs.sqlalchemy.org/en/latest/orm/examples.html#module-examples.dogpile_caching

他のヒント

2番目の質問に対する回答ではありませんが、このリンクのコメントから、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.

SQLAlchemyは2種類のキャッシュをサポートしています:

  1. 結果セットをキャッシュして、同じクエリを繰り返し実行すると、データベースではなくキャッシュがヒットするようにします。 memcached redis 、および基本的なフラットファイルを含む多くの異なるバックエンドをサポートする dogpile を使用します。

    ドキュメントは次のとおりです。 http:// docs。 sqlalchemy.org/en/latest/orm/examples.html#module-examples.dogpile_caching

  2. Pythonインタープリターが毎回手動でクエリ文字列を再構築する必要がないように、 query オブジェクトをキャッシュします。これらのクエリは bakedクエリと呼ばれ、キャッシュは baked と呼ばれます。基本的に、データベースにアクセスする前に sqlalchemy が実行するすべてのアクションをキャッシュします。データベース呼び出しを削減しません。初期ベンチマークでは、 query 生成時間の最大40%の高速化が示されていますが、コードの冗長性がわずかに増加するというトレードオフがあります。

    ドキュメントはこちら: http://docs.sqlalchemy.org/ ja / latest / orm / extensions / baked.html

または弱参照辞書(weakref.WeakValueDictionary)を介してアプリケーションレベルのキャッシュを使用する場合は、次の例を参照してください: http://www.sqlalchemy.org/trac/wiki/UsageRecipes/UniqueObject

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top