artsy / garner

A set of Rack middleware and cache helpers that implement various caching strategies.
MIT License
344 stars 24 forks source link

Caching the Index method #94

Closed AndreiDev closed 8 years ago

AndreiDev commented 8 years ago

Hi! In my Orders controller, there's an index method that returns all orders for a specific account. Is there a way to cache calls like current_account.orders (get all orders that the current_account has), invalidating only when the number of orders for current_account changes? Thanks!

dblock commented 8 years ago

You could construct a key manually that is a combination of current account ID and, say, "orders", then invalidate that cache entry whenever an order affects an account. Garner doesn't help that much here otherwise.

If you create an Order without touching the account record at all, then you'd have to bind on any order, including when someone else's account changes. I suggest touching the current_account and then just doing garner.bind(current_account) and calling it a day.

dblock commented 8 years ago

I'll close this, let us know whether you have more questions!