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

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

问题描述

我想将查询的结果缓存在ember-data中。 ( findQuery



要清楚:我不想缓存整个模型;只是是什么样的模型是查询的结果。这是正确的地方在哪里?



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



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



其实我不想要 findQuery 被调用用于特定类型的查询。喜欢 findAll 的行为。尼斯会像一个 queryShouldBeCached 钩子。



有没有一个很好的解决方案?

解决方案

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



给予Ember数据查询返回Promises,例如 return this.store.findAll('blog-post'); // => Promise ,我们可以在一个具有更高阶函数的简单对象(返回函数的函数)中缓存承诺。对象缓存可以替换为 localStorage sessionStorage Map 甚至 WeakMap ,但我正在使用对象缓存来使事情变得简单易懂。



你想要什么基本上做的是替换以下调用:

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

有些东西或多或少如下:

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

实际上,下面的解决方案可能看起来更像:

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

因此,您将要求数据一次,并始终在后续调用中从缓存返回。 >

让我向您展示如何执行可能如下所示:

  var cachedStore = (function(){
//您的缓存 - 在这种情况下为简单对象字面值
var cache = {};

//尝试之前检查缓存结果的实际方法查询服务
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];
};
}());

以下是一个示例用法:

  //触发一个请求
cachedStore.call(this,'findAll','blog-post');
//从缓存返回
cachedStore.call(this,'findAll','blog-post');
//从缓存返回
cachedStore.call(this,'findAll','blog-post');

//触发请求
cachedStore.call(this,'findRecord','blog-post',123);
//从缓存返回
cachedStore.call(this,'findRecord','blog-post',123);
//从缓存返回
cachedStore.call(this,'findRecord','blog-post',123);

有什么帮助吗?


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?

解决方案

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?

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

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