缓存来自远程服务器的 xml 数据
假设我正在使用 simpleXML 解析来自远程服务器的天气数据,然后远程服务器崩溃了,因此我无法再恢复其实时提要,但我也不希望我的用户收到错误消息,我该怎么办关于缓存并继续显示我在服务器崩溃之前从服务器获取的最后一条数据?
假设我的 xml 如下所示:
<weather>
<temperature c="25" f="77">
</weather>
在能够重新建立与远程服务器的连接之前,我将如何显示值“25”和“77”?
如果我的问题不完全清楚,我很抱歉......我对服务器端技术的了解非常有限。
Let's say I'm using simpleXML to parse weather data from a remote server, and then the remote server crashes so I can no longer recover its live feeds but I don't want my users to get an error message either, how would I go about caching and continuing to display the last piece of data I got from the server before it crashed?
Let's say my xml looks like this:
<weather>
<temperature c="25" f="77">
</weather>
How would I go about displaying the values "25" and "77" until I'm able to reestablish a connection with the remote server?
Apologies if my question isn't entirely clear... my knowledge of server-side technologies is very limited.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
第一:当用户请求您的站点时,您不希望实时获取远程数据。这适用于没有问题时访问量很少的小型网站,但一旦远程服务器挂起,您的网站也会挂起,直到发生连接超时。
我们主要做的事情如下:
最后,您的页面将始终快速交付,并且在远程服务器关闭时不会崩溃。
First: You do not want to fetch the remote data live when the user requests your site. That works for small sites with few visitors when no problems occur, but as soon as the remote server hangs, your site will also hang until the connection timeout occurs.
What we mostly do is the following:
In the end, your pages will always be delivered fast and will not crash when the remote server is down.
这不是最好的方法,但这是您可以做到的一种方法:
保存信息
加载信息
通过包含该文件,您的 $c 和 $f 变量将被设置,除非您覆盖它们。
This isn't the best way, but here is one way you could do it:
To save the information
To load it
By including the file, your $c and $f variables will be set unless you overwrite them.
在本地存储这些值并向您的用户显示此信息。当你想要的时候更新,这样只有成功后才会覆盖本地副本;如果失败,您将拥有“本地”副本。
Store the values locally and display this information to your users. Update when you want, in such a way that it will only overwrite the local copy when successful; If it fails, you will have your 'local' copy.