Counter Caches

3 views
Skip to first unread message

Jacques Crocker

unread,
Dec 18, 2008, 4:54:26 AM12/18/08
to DataMapper
Hi. I really need some counter caches in some of my models (to keep
track of a few "has n" relations) and I haven't been able to find any
standard or plugin solutions. Is there one available, or does everyone
using datamapper currently do it themselves with callbacks?

Also, let me know if you guys consider counter caches bad practice or
something. Seems strange that its not built in.

ban there

unread,
Dec 18, 2008, 5:42:25 AM12/18/08
to DataMapper
I'm using dm-observer + dm-adjust instead of ar's counter cache.

Jacques Crocker 작성:

Dan Kubb (dkubb)

unread,
Dec 18, 2008, 10:33:06 AM12/18/08
to DataMapper
Hi Jacques,

> Hi. I really need some counter caches in some of my models (to keep
> track of a few "has n" relations) and I haven't been able to find any
> standard or plugin solutions. Is there one available, or does everyone
> using datamapper currently do it themselves with callbacks?

afaik, there's no plugin to do this but it would be relatively simple
to assemble one using existing plugins.

> Also, let me know if you guys consider counter caches bad practice or
> something.

When you add a counter cache you're denormalizing the data, which I
would not consider unless there was an actual need, proven through
benchmarking before and after adding the cache. IMHO denormalizing
without first proving you need to do so is a code smell. However, I
know there are legitimate cases where it can boost performance but you
need to test first.

> Seems strange that its not built in.

Everything in DataMapper is based on real need, and features are only
added to dm-core if they need to be in core, otherwise it should be a
plugin. In this case it would be easy to assemble a counter cache
using hooks and dm-adjust, as already mentioned in this thread. I can
only assume that the only reason it hasn't been packaged into a plugin
yet is that it's either not common enough, or assembling it using
available primitives is simple enough that it's not caused anyone
enough pain.

Dan
(dkubb)
Reply all
Reply to author
Forward
0 new messages