2008

2007

2006

2005

2004

SQLAlchemy caching (second level)

▁ jan 01 2008

SQLAlchemy is an Object Relational Mapper (ORM) for Python, supporting a wide variety of databases.

As per the 0.4 documentation, it mentions that:

Is the Session a cache? Yeee…no. It’s somewhat used as a cache, in that it implements the identity map pattern, and stores objects keyed to their primary key. However, it doesn’t do any kind of query caching.

Admittedly, it would be an extremely daunting and challenging task to write a caching framework that would work well for all use cases. Yesterday, after some googling, I decided to write my own cache. As of version 0.4.1, there are some features that makes it possible to do this rather easily; you can write a MapperExtension that overwrites the ‘get()’ method that SQLAlchemy usually uses to fetch data. There you can cache your things. I’m doing this in memcached.

It’s built heavily on this thread from the SQLAlchemy mailinglist.

Oh, here’s the code.

How to use? Like this:

mapper(Foo, t_foo, extension=MemCachedMapper(mc, timeout=35))

Easy, eh? Now just use the session.query(Foo).get(xyz), and you’ll be caching results for 35 seconds (the second named argument to MemCachedMapper.) ‘mc’ is an instance of memcached or cmemcached, that is already connected.

This is in semi-production on my own pet-project, but I’d really appreciate some feedback from anyone who attempts to use this in their own code. If/when you run into a bug, write me here, and I’ll do my best to fix it.

← Previous: Adaptive Replacement Cache in Python  //  Next: Coffee @ home

comments

Daniel Ellison, 2 months, 2 weeks ago:

Does this work with SQLAlchemy 0.5 as well? I’m just starting a project which will use SQLAlchemy to access a MS SQL Server database, and I want to make sure there’s some sort of caching in place. But I’m currently quite unfamiliar with SQLAlchemy, so I can’t just glance at your code and determine if it’s suitable.

Thanks!

Jesper Noehr, 2 months, 2 weeks ago:

Daniel,

I don’t know, actually, haven’t used SA in a while now. You can give it a try, but YMMV.

powered by