Ember-Data .find()vs .all() - 如何控制缓存? [英] Ember-Data .find() vs .all() - how to control cache?

查看:72
本文介绍了Ember-Data .find()vs .all() - 如何控制缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被告知为了不要一直发出请求,可以使用.all()方法来加载保存在商店中的数据。但是Ember如何处理缓存?我有几个问题。

I was told that in order to not make a request all the time, one can use .all() method to load data that is kept in the store. But how does Ember deal with cache? I have a couple of questions.

如何控制缓存?什么时候使用.find()和.all()。你是否使用.find()然后.all()?多长时间?

How do you control cache? When do you use .find() and when .all(). Do you use .find() and then .all()? For how long?

.all()是否有一些到期日期,以便在一段时间后可以发出新的请求?或者使用本地存储,以便我必须手动清除?

Does .all() have some expiration date so that after some time it can make a new request? Or it uses Local Storage so that I have to clear it manually?

假设我有一些数据,我想每周只刷新一次?我该怎么办?现在每次进入或重新访问同一条路线时,都会发出新的请求。如何避免这种情况?

Suppose that I have some data I'd like to refresh only once a week? How should I go about this? Now every time I enter or re-visit the same route a new request is made. How can I avoid this?

推荐答案

所以首先从您的评论中回答问题:

So will start by answering question from your comment:


我想知道如何在应用程序启动时加载数据(不是通过路由,因为我不必经常更新它)。是可能的

I'd rather to know how can I load data when an app starts (not via routes as I don't have to update it so often). Is it possible

所以在技术上这还是通过路线,但是在应用程序启动时加载数据的最佳方式是通过应用程序路由的模型钩子。

So OK technically this is still via routes, but the best way to load data when an app "starts" is via the Application Route's model hook.

App.ApplicationRoute = Ember.Route.extend({
  model: function({
    return App.Post.find();
  })
})

路由器将等待find()返回的承诺来解决,所以您可以确保在输入任何其他路由之前,服务器的响应已经恢复。

The router will wait for promise returned by find() to resolve, so you can be sure that response from server has come back before any other routes are entered.

如何控制缓存?

主要是你不用担心。如果需要,您可以在某些超时后刷新()个别记录。

Mostly you don't worry about it. You can refresh() an individual record after some timeout if needed.


什么时候使用.find()和.all() 。你是否使用.find()然后.all()?多长时间?

When do you use .find() and when .all(). Do you use .find() and then .all()? For how long?

取决于您想要实现的目标。在我们的应用程序中,我们在应用程序路由中使用find(),然后在其他路由中使用all()或filter()。

Depends what you want to achieve. In our app we use find() in the application route, then either all() or a filter() in other routes.


.all()有一些到期日期,以便在一段时间后可以发出新的请求?

Does .all() have some expiration date so that after some time it can make a new request?

没有。它不会发出新的请求

Nope. It will never make a new request


或者使用本地存储,以便我手动清除

Or it uses Local Storage so that I have to clear it manually?

它不使用本地存储,记录在内存中。所以肯定F5将会清除缓存。

It does not use local storage, records are in memory. So for sure an F5 will clear the cache.


假设我有一些数据,我只想刷新一周一次?我该怎么办?现在每次进入或重新访问同一条路线时,都会发出新的请求。如何避免这种情况?

Suppose that I have some data I'd like to refresh only once a week? How should I go about this? Now every time I enter or re-visit the same route a new request is made. How can I avoid this?

所以,让我们假设您只在应用程序路由中使用find(),该用户保持浏览器的开放持续1周,记录已过期。有很多方法可以刷新,什么是容易/最好取决于它们是否一次过期,或者一次超时。

So OK let's assume you use find() only in the application route, and that user keeps browser open for 1 week and the records have expired. There are many ways to refresh, what's easy/best depends on if they all expire at once or if they time-out one at a time.


  • 有一些计时器检查过期的记录,并根据需要调用refresh()。

  • Ping模式,您在某些时间表上进行更新。当服务器响应更新时,它可以加载任何更改的记录。

  • 或者可以每周刷新浏览器一次(通过window.location ...)

这篇关于Ember-Data .find()vs .all() - 如何控制缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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