简体   繁体   English

如何在iPhone应用程序中缓存Javascript和JSON数据?

[英]How can I cache Javascript and JSON data in my iPhone app?

I am developing a native iPhone app in Titanium. 我正在使用Titanium开发本机iPhone应用程序。 Within this app I am using data from a remote API (which I have developed in Rails 3). 在这个应用程序中,我正在使用来自远程API(我在Rails 3中开发)的数据。 I want the user to cache the API data on their phones as much as possible. 我希望用户尽可能在手机上缓存API数据。 What I need help with is the concept of caching. 我需要帮助的是缓存的概念。 What is the best way of doing it? 最好的方法是什么? The nature of the data in the API is that it needs to be up to date. API中数据的性质是它需要是最新的。 Because it is contact data that can change anytime. 因为联系数据可以随时更改。

I have no clue in how the cache process would work. 我对缓存过程如何工作一无所知。 If you someone can explain the best way of managing a caching process for the API I would be more than happy! 如果您能解释管理API缓存过程的最佳方法,我将非常高兴!

I am using JSON and Javascript. 我正在使用JSON和Javascript。

"The nature of the data in the API is that it needs to be up to date. Because it is contact data that can change anytime" “ API中数据的性质是它必须是最新的。因为联系数据可以随时更改”

If that's true then it makes any kind of caching redundant, as you would need to compare the cache to live data to check for changes, thus making the cache itself pointless. 如果是这样,那么它会使任何类型的缓存变得多余,因为您需要将缓存与实时数据进行比较以检查更改,从而使缓存本身毫无意义。

The only reason you may still want to cache the data is to have it available off-line. 您可能仍想缓存数据的唯一原因是使数据脱机可用。 That being the case i would use an SQLite database, which is native to the iphone. 既然如此,我将使用iphone本身的SQLite数据库。

titanium-cache is clean code with a unit tests and provides some sample code in the readme. Titanium缓存是带有单元测试的简洁代码,并在自述文件中提供了一些示例代码。 I integrated this with my own project in a matter of minutes and it worked nicely. 我在几分钟内将其与自己的项目集成在一起,并且效果很好。

I think the type of cache it's application dependent. 我认为缓存的类型取决于应用程序。

You can cache data on: 您可以在以下位置缓存数据:

  1. client; 客户;
  2. server; 服务器;
  3. other network element. 其他网络元素。

Critical point is refresh of data. 关键是数据的刷新。 A bad algorithm produce inconsistent data. 错误的算法会产生不一致的数据。

You can find interesting information on literature of distributed systems 您可以找到有关分布式系统文献的有趣信息。

Bye 再见

A couple options here. 这里有几个选择。

1) You can use ASIHTTPRequest and ignore cache headers to cache everything. 1)您可以使用ASIHTTPRequest并忽略缓存头来缓存所有内容。 When your app is being used, you can detect if the cache is being hit. 使用您的应用程序时,您可以检测是否正在缓存。 If it is hit, you fire off a request to the server after the cache hit to request any new data. 如果命中,则在命中高速缓存后向服务器触发请求以请求任何新数据。 You can do this by appending a random URL param to the end of the URL since the cache keys off of the URL. 您可以通过在URL的末尾附加一个随机URL参数来实现此目的,因为缓存是从URL退出的。 If you have a good connection and new data, load it in. Otherwise do nothing and your user has the latest data when using the app under a good connection 如果您拥有良好的连接和新数据,请加载它。否则,不执行任何操作,并且在良好连接下使用该应用程序时,您的用户拥有最新数据

2) Do most of #1 by always hitting the cache but instead of firing a non-cachable version of the same request to the server after hitting the cache, fire off a non-cacheable timestamp check to see if data was updated. 2)通过总是命中高速缓存来执行#1的大部分操作,而不是在命中高速缓存后向服务器触发同一请求的不可访问版本,而是启动不可高速缓存的时间戳检查以查看数据是否已更新。 If it has been, fire off the non-cachable full API request. 如果已经存在,请触发不可缓存的完整API请求。 If it hasn't or it fails, you can do nothing. 如果没有,或者失败,您将无能为力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM