[CDBI] Memcached

Perrin Harkins pharkins at gmail.com
Fri Jan 26 20:07:33 GMT 2007

On 1/26/07, Derek Watson <watson at wayspa.com> wrote:
> Memcached is a key component in making some of the web's most heavily
> trafficked sites possibe: (http://www.danga.com/memcached/users.bml)
> and it alarms me that I can't find anyone using it with CDBI.
> Furthermore, it's sad that 2 pillars of the Perl community would
> discourage it's usage.

Take it easy, Derek.  No one is telling you not to use memcached.

To answer your larger question first, yes, if you want a tool that has
active development for features like this, you should probably be
looking at Rose::DB::Object or DBIx::Class rather than Class::DBI.
Those tools also generate more efficient SQL for many common cases,
like deleting multiple objects.

I do encourage people to look hard at their database before reaching
for a caching solution.  Caching adds a lot of complexity to your
system, and it's generally not something you should do if you have a
choice.  You'll have more code, new kinds of bugs, and new points of

You sound like you realize this already, but I often see people reach
for caching as a quick fix solution rather than analyzing what's going
wrong.  I think the first step should be profiling, and then
query/database tuning, and then caching.

In terms of how best to integrate caching into your database objects,
I haven't seen that much benefit from caching primary key lookups,
which is what you do when you intercept retrieve() and update() calls.
 On that kind of lookup, MySQL can be faster than memcached.  (Of
course there is an advantage to keeping queries off your database...)
If you want that though, Class::DBI::Sweet has it, and so does
Class::DBI::Cacheable (but not with memcached).  I can't vouch for
either, since I don't use them.  Ugly?  Probably.  It's not something
Class::DBI was built for.

The bigger bang for the buck comes from caching complicated search
queries.  This gets trickier, since you now have to worry about the
cached result being invalid after a change to one of the contained
objects, but that's the nature of caching: it's a decision to return
incorrect data sometimes, in order to improve performance.
Class::DBI::Sweet supports this too, under the name
"result_set_cache."  I couldn't tell you if anyone has really used it
though.  It has not really been discussed on this list.

Good luck, and if you discover anything useful, I'm sure the list
would like to hear about it.

- Perrin

More information about the ClassDBI mailing list