adelevie/parse_resource

Memcache?

Closed this issue · 4 comments

Hey there,

Has anyone looked into using memcache to temporarily cache objects from parse_resource? Marshal.dump & Marshal.load seem to work fine on objects, so it should just be a matter of wrapping things, but I figured I'd ask before going off on my own.

Thanks!

I haven't, but this would be an awesome addition. Would you make a pull request?

Yeah I'll mull it over this week. What do you think something like this should look like?

For this class:
class CalendarEvent < ParseResource::Base; end

One way:
CalendarEvent.cache('criteria-all-key', expires_in: 10.minutes) { where(criteria).all }
CalendarEvent.cache('first-key', expires_in: 10.minutes) { first }
CalendarEvent.cache('last-key', expires_in: 10.minutes) { last }

Another where cache key is generated from criteria and cached implies calling all:
CalendarEvent.where(criteria).cached(expires_in: 10.minutes)
CalendarEvent.first_cached(expires_in: 10.minutes)
CalendarEvent.last_cached(expires_in: 10.minutes)

Finally, have it be a scope option:
CalendarEvent.where(criteria.merge(cache: {expires_in: 10.minutes})).all
CalendarEvent.first(cache: {expires_in: 10.minutes})
CalendarEvent.last(cache: {expires_in: 10.minutes})

First one leads to less surprises, the second one feels better, the third probably isn't as much work as the second.

I like the second one a lot. Even:

CalendarEvent.cached(10.minutes).first

Oh right that would work too... Ok I mull it over. Thanks!