Find cached methods

I’m working on an application whose a bunch of records are only updated (backend side) once a day. It seems totally useless to request the server (via the store) every time the app need to get all these kind of records.

I’ve reopened the DS.Store and added two “find cached” methods with cache expiration options :

  • findAllCached to get all records
  • findQueryCached to query some records

Both methods have expiration options :

  • for, to define an expiration period
  • until, to define an expiration date

These methods request the server only if records are not yet cached.

findAllCached is based on findAll and all DS.Store methods.

findQueryCached is based on findQuery method plus a cache system linked to model metadata object.

Examples :

 this.store.findAllCached("user", { for: moment.duration(1, "hours") });
 this.store.findAllCached("user", { until: moment().add(1, "hours") });

 this.store.findQueryCached("post", { filter: "active" }, { for: moment.duration(1, "hours") });
 this.store.findQueryCached("post", { filter: "active" }, { until: moment().add(1, "hours") });

Here is a gist of this draft hack :

https://gist.github.com/bencolon/7f062cb28d7b82cd2038

And a JsBin with a basic use case. Cars records are cached for one hour VS Trucks records are not cached.

http://jsbin.com/pogali

What you guys are thinking about ? Useless ? Useful ?

Can’t wait to hear the Ember community about that.