0

For bar_id = 1 a cache gets formed on first access. but even if bar_id is changed to 2 later on the values of 1 is retained. How do I make it work across bar_id(s)

 @foo = Rails.cache.fetch("foo_by_id", expires_in: 10.minutes) do
    Foo.where(:search => params['search'], :bar_id => params['bar_id']).first
 end
Rpj
  • 5,348
  • 16
  • 62
  • 122

1 Answers1

1

Your cache key must include any dynamic data you want to separate your cache by. In your case the cache key must include bar_id. What I'd do is:

@foo = Rails.cache.fetch("foo_by_id:#{params['bar_id']}", expires_in: 10.minutes) do
    Foo.where(:search => params['search'], :bar_id => params['bar_id']).first
 end
Erez Rabih
  • 15,562
  • 3
  • 47
  • 64
  • Thanks very much Erez, am pretty sure that this is the right fix. Are there any other patterns which I can use with respect to caching – Rpj Mar 29 '13 at 14:20
  • 1
    @Rpj it is tell since your question is too general. If you have anything which is more concrete please post another question and I'd be glad to try and help. – Erez Rabih Mar 29 '13 at 14:49