Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Touching the counter_cultured model #25

Closed
giuseb opened this issue Sep 22, 2013 · 2 comments
Closed

Touching the counter_cultured model #25

giuseb opened this issue Sep 22, 2013 · 2 comments

Comments

@giuseb
Copy link

giuseb commented Sep 22, 2013

It would be nice if updating the counters also set the model's updated_at field to the current time, in order to facilitate cache expiration.

Imagine a Category index page listing category names and product counts. These days, it is trivial to perform http caching and/or cache the view using a key that includes the most recent change to the categories.

# controller
fresh_when last_modified: @categories.maximum(:updated_at)

# haml view
- cache ['categories', @categories.maximum(:updated_at)] do
  = the view template

Now, if I add/remove a product or change a product's category, I also need to touch the affected categories, otherwise the category index will be stale. Normally, you would enforce this like so:

belongs_to :category, touch: true

HOWEVER, this is too general, as it will cause the cache to needlessly expire when, for example, I am only editing a product's name or other attribute.

Thus, ideally I would like to be able to say something like:

counter_culture :category, touch: true

Does this sound reasonable? I looked at the source, and I understand that you eventually call rails' update_counters AR method, so the fix would be a little more involved than I initially hoped...

Thanks!
Giuseppe

@magnusvk
Copy link
Owner

It does sound reasonable. Not trivially easy (because I use
update_counters) but not really that hard either. I'll try and get to it
soon, though no promises. Otherwise, you are always free to submit a pull
request. :)

Thanks for getting in touch,
-Magnus

On Sun, Sep 22, 2013 at 4:30 AM, Giuseppe notifications@github.com wrote:

It would be nice if updating the counters also set the model's updated_at
field to the current time, in order to facilitate cache expiration.

Imagine a Category index page listing category names and product counts.
These days, it is trivial to cache the action and/or the view using a key
that includes the most recent change to the categories.

Now, if I add/remove a product or change a product's category, I also need
to touch the affected categories, otherwise the category index will be
stale. Normally, you would enforce this like so:

belongs_to :category, touch: true

HOWEVER, this is too general, as it will cause the cache to needlessly
expire when, for example, I am only editing a product's name or other
attribute.

Thus, ideally I would like to be able to say something like:

counter_culture :category, touch: true

Does this sound reasonable? I looked at the source, and I understand that
you eventually call rails' update_counters AR method, so the fix would be
a little more involved than I initially hoped...

Thanks!
Giuseppe


Reply to this email directly or view it on GitHubhttps://github.com//issues/25
.

@giuseb
Copy link
Author

giuseb commented Oct 6, 2013

I had forked the project in view of attempting a solution, but this merge is such a treat!
Thanks so much for your work,
Giuseppe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants