mhgbrown / cached_resource

Caching for ActiveResource
MIT License
80 stars 28 forks source link

Add object to cached collection #20

Open samiljin opened 9 years ago

samiljin commented 9 years ago

Hey! I'm not sure if there is already way to to this but didn't find anything at documentations.

If I have an cached collection like:

objects = Object.find(:all)

And I create a new object object = Object.create(params) it would be nice if I was able to add that newly created object into cached collection called objects.

mhgbrown commented 9 years ago

ah interesting! Do you have a specific use case that you are looking at?

samiljin commented 9 years ago

Hey! Actually yes. In my application there are collection @items that has about 700-1000 items inside it. The items gets loaded from external API when user signs in and then are cached. User is able to create new items and that's where the problem is, because I have to clear the whole cache to get the newly created item to the collection.

It would be awesome if I was able to do something like this:

item = Item.create(params)
@objects << item

Instead, I have to:

item = Item.create(params)
Item.clear_cache
@objects = Item.find(:all)

and the application gets slower and there are much more load to the external API.

mhgbrown commented 9 years ago

I see, I see. Maybe a side effect of the create method could be to add it to the collection cache? Any thoughts about how it might work? You show explicitly adding it in your example.

samiljin commented 9 years ago

I'm not sure exactly what you mean, but i tried to add newly created object to the collection @objects like: @objects.elements << item and it works for the next page load perfectly. But after i navigate further, the newly created object disappears from @objects. So I'm able to add it there but it's not cached.

mhgbrown commented 9 years ago

Gotcha Ok. Let me explore this a bit. I think this is a cool idea!