37

I want to cache the result of a query in ember-data. (findQuery)

To make it clear: I don't want to cache the entire models; just what models are the result of the query. Where is the right place for this?

I was thinking about implementing this in the adapter and cache the result of the AJAX call, but I don't think this is a good solution since I don't wanna override the loaded and maybe newer and/or modified model data.

I don't thinks its possible to just return a list of ID's, and to manipulate the adapter and the serializer for this simple use-case seems to be messy!

Actually I don't want that findQuery is called for specific types of querys. Like the behavior of findAll. Nice would be something like a queryShouldBeCached hook.

Is there a good Solution for this?

Liam
  • 22,818
  • 25
  • 93
  • 157
Lux
  • 16,183
  • 3
  • 36
  • 64
  • Where are you using the cache, i.e. on a particular route, and the cache would be cleared when the query was re-run or there's a transition away from the route? – aceofspades May 17 '15 at 00:21
  • there is a transition away from the route. The use-case is that i display a list of search results, the user clicks on a result, and transition to the detail page. if he goes back i don't want to re-run the search wich costs almost 5 seconds. – Lux May 21 '15 at 11:47
  • 1
    I hook into the `setupController` and `deactivate` hooks of the route, where I grab a reference to the controller and cache full records there. In my case I destroy them when I transition out, so you'd have to consider how to trigger a deferred cleanup. I believe it belongs in the route/controller, rather than the adapter which is more of a abstract/global thing. This way it's contained for the specific function (search). – aceofspades May 21 '15 at 15:19

2 Answers2

2

I'm not an Ember expert but I think you can address your problem with a pure JS solution.

Given Ember Data queries return Promises, e.g. return this.store.findAll('blog-post'); // => Promise, we can cache promises in a simple object with higher order functions (functions that return functions). The object cache could be replaced with localStorage, sessionStorage, Map or even WeakMap but I'm using the object cache to make things simple to understand.

What you want to essentially do is to replace following call:

return this.store.findAll('blog-post');

with something more or less like:

return cachedStore.findAll('blog-post');

actually, with the solution below it might look more like:

return cachedStore.call(this, 'findAll', 'blog-post');

As a result, you will request data once and always return from cache in subsequent calls.

Let me show you how the implementation might look like:

var cachedStore = (function () {
  // Your cache - in this case simple object literal
  var cache = {};

  // Actual method that will check cache for results before trying to query services
  return function (method) {
    var args = Array.prototype.slice.call(arguments, 1);
    var serializedArgs = JSON.stringify(args);

    if (!(serializedArgs in cache)) {
      cache[serializedArgs] = this.store[method].apply(this, args);
    }
    return cache[serializedArgs];
  };
}());

And here's a sample usage:

// Fires a request
cachedStore.call(this, 'findAll', 'blog-post');
// Returns from cache
cachedStore.call(this, 'findAll', 'blog-post');
// Returns from cache
cachedStore.call(this, 'findAll', 'blog-post');

// Fires a request
cachedStore.call(this, 'findRecord', 'blog-post', 123);
// Returns from cache
cachedStore.call(this, 'findRecord', 'blog-post', 123);
// Returns from cache
cachedStore.call(this, 'findRecord', 'blog-post', 123);

Does that help in any way?

  • We could have made the api much nicer than `cachedStore.call(this, 'findAll', ...)`, e.g. `this.cachedStore.findAll(...)`. I made it like this because I didn't really know where exactly in your application you want to hook with the caches (single file only? a whole application?). Also, you might want to explore things even more and make results expire automatically after given period of time. There are lots of options unexplored in the example above, just wanted to show the direction. If anything needs further explanation, please let me know – Maciej Smoliński Nov 16 '16 at 08:45
0

When you fire a request based on params Ember Data doesn't know how those params necessarily translate into the models (aka it doesn't know that you have all of the records that have some sort of relationship param1). You can cache it yourself, but then you'd still need some sort of way of knowing those records from other records in your store.

  • 1
    I know it because thie param that I'm filtering is not editable by the user. But how can I cache it? Assume that I want to cache all query requests. – Lux Jun 11 '15 at 09:58