views:

1350

answers:

4

Does SQLAlchemy support some kind of caching so if i have lots of time the same query it would return the response from cache instead of querying the database until i clear the cache for that query because i updated de db?

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

+4  A: 

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.
torial
This link http://www.mail-archive.com/[email protected]/msg15667.html suggests/shows that the subsequent query doesn't use a query but returns the instance from the identity map but this only works if you are querying using the primary key.
andho
+1  A: 

No, but you can do caching with memcache.

Lucky
+1  A: 

Or use an application-level cache via weak references dicts (weakref.WeakValueDictionary), see an example here : http://www.sqlalchemy.org/trac/wiki/UsageRecipes/UniqueObject

ychaouche
+6  A: 

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/06/examples.html#module-beaker_caching .

zzzeek