views:

234

answers:

0

Suppose I have a mapped class Article and I have a mapped class Author. Article.author is a relation on Author that returns the author of the Article. How do I make Article.author to cache the value in memcached?

The problem is that referring to Article.author does an SQL query each time it is called, but there is really no need to query the database for author every single time.

This value can be cached in memcached and returned instantly from memory.

What I can't figure out is how to cache the value of relation in SQLAlchemy?

I could create another property, like Article.cached_author which would first consult memcached for key article_<id>_author, and if it's not there, call Article.author, and store this value value in memcached.

But I don't want to invent new names for my properties.

Can anyone help me figure out how to use the SQLAlchemy relation property names and still cache them with memcached?

Thanks, Boda Cydo.

Ps. Some people have said that SQLAlchemy caches the values already, but that is not true! It caches the value within a single session but I am talking here about tens of sessions using my program simultaneously.

Pss. Please also don't assume that I have single property Article.author, I have various properties, like Article.keywords that returns a long list of keywords associated with Article. If I don't cache the values returned by accessing these properties, the application will soon kill the computer it is running on because of requesting the same data over and over and over again from the database.