Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does SQLAlchemy support caching?

Does SQLAlchemy support some kind of caching so if I repeatedly run the same query it returns the response from cache instead of querying the database? Is this cache automatically cleared when the DB is updated?

Or what's the best way to implement this on a CherryPy + SQLAlchemy setup?

like image 737
daniels Avatar asked Oct 15 '08 14:10

daniels


People also ask

Does SQLAlchemy cache query?

SQLAlchemy supports two types of caches: Caching the result set so that repeatedly running the same query hits the cache instead of the database. It uses dogpile which supports many different backends, including memcached , redis , and basic flat files.

Does SQLAlchemy support batching?

SQLAlchemy supports the widest variety of database and architectural designs as is reasonably possible. Unit Of Work. The Unit Of Work system, a central part of SQLAlchemy's Object Relational Mapper (ORM), organizes pending insert/update/delete operations into queues and flushes them all in one batch.

Should I use SQLAlchemy core or ORM?

If you want to view your data in a more schema-centric view (as used in SQL), use Core. If you have data for which business objects are not needed, use Core. If you view your data as business objects, use ORM. If you are building a quick prototype, use ORM.


2 Answers

We have a pretty comprehensive caching solution, as an example in conjunction with embedded hooks, in 0.6. It's a recipe to subclass Query, make it aware of Beaker, and allow control of query caching for explicit queries as well as lazy loaders via query options.

I'm running it in production now. The example itself is in the dist and the intro documentation is at http://www.sqlalchemy.org/docs/orm/examples.html#beaker-caching .

UPDATE: Beaker has now been replaced with dogpile caching: http://docs.sqlalchemy.org/en/latest/orm/examples.html#module-examples.dogpile_caching

like image 74
zzzeek Avatar answered Sep 25 '22 18:09

zzzeek


Not an answer to your second question, but from the comments in this link indicates that SQLAlchemy does not support cacheing : http://spyced.blogspot.com/2007/01/why-sqlalchemy-impresses-me.html

Raven said...

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 

Jonathan Ellis said...

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. 
like image 43
torial Avatar answered Sep 21 '22 18:09

torial