如何在 ember 数据中缓存查询结果 [英] How to cache query result in ember data

查看:15
本文介绍了如何在 ember 数据中缓存查询结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 ember-data 中缓存查询结果.(findQuery)

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?

我正在考虑在适配器中实现它并缓存 AJAX 调用的结果,但我认为这不是一个好的解决方案,因为我不想覆盖加载的和更新的和/或修改的模型数据.

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.

我认为不可能只返回一个 ID 列表,并操纵适配器这个简单用例的序列化程序似乎很混乱!

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!

实际上我不希望为特定类型的查询调用 findQuery.就像 findAll 的行为.很好,类似于 queryShouldBeCached 钩子.

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?

推荐答案

我不是 Ember 专家,但我认为您可以使用纯 JS 解决方案来解决您的问题.

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

给定的 Ember 数据查询返回 Promises,例如return this.store.findAll('blog-post');//=>Promise,我们可以将 Promise 缓存在一个带有高阶函数(返回函数的函数)的简单对象中.对象缓存可以替换为 localStoragesessionStorageMap 甚至 WeakMap 但我正在使用对象缓存以使事情更容易理解.

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');

或多或少像:

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];
  };
}());

这是一个示例用法:

// 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);

这有什么帮助吗?

这篇关于如何在 ember 数据中缓存查询结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆