问题描述
我有一个asp.net mvc应用程序,该应用程序返回JSON结果,其中包含多达n年的数据,然后将其呈现在Javascript图表上.
I've a asp.net mvc application which returns the JSON result containing upto n number of years worth of data which then gets rendered on Javascript chart.
为了获得良好的用户体验(就性能而言),我正在寻找最佳的解决方案,即是否可以在客户端缓存JSON数据,以便当用户单击具有不同参数(例如日,周)的Chart时视图等,无需访问服务器即可查询相同的JSON数据.
In order to have a good user experience (in terms of performance) I'm looking for the best solution whether it's possible to cache the JSON data on client side so when user clicks on Chart with different parameters such as day, week view etc, the same JSON data is queried without hitting a server.
请问有人可以帮助我们对缓存最佳做法做出最佳决定,该做法是应该在客户端还是在服务器端缓存数据,还是应该在每个图切换时直接访问数据库?
Could someone please help us to make a best decision on caching best practices on whether the data should be cached on client side or server side or should directly hit database for each graph toggle?
谢谢.
推荐答案
首先,数据库在哪里?如果您位于具有千兆位局域网的本地网络上,那么点击它就不会有问题.但是,在互联网上并非如此.人们的带宽有限,尤其是在移动设备上,带宽有限,因此您应该限制HTTP呼叫.另外,更少的HTTP调用意味着对服务器的减轻.
First of all, where is the database? If you are on a local network with gigabit LAN, then hitting it won't be a problem. However, that is not true over the internet. People have limited bandwidth, especially on mobile, and thus you should limit your HTTP calls. Also, less HTTP calls means less strain on the server.
以下是一些提示:
-
考虑分页
Consider pagination
当加载"2年价值"时,我想像很多,就像100多页的论文一样.考虑对数据分页而不是一次全部加载.这样可以节省带宽和缓存空间(如果有限制的话).
When loading "2 years worth", I imagine a lot, like a 100+ page thesis. Consider paginating data instead of loading them all at once. This saves you bandwidth as well as cache space (If ever it's limited).
方法:让服务器脚本根据客户端的需求切分数据.使用查询中的LIMIT
在SQL中创建分页非常容易.逻辑就像starting_item = (page_needed - 1) * items_per_page
How to: Have the server script slice up the data according to what the client wants. It's pretty easy to create pagination in SQL using LIMIT
in the query. The logic is like starting_item = (page_needed - 1) * items_per_page
JSONify数据
使用JSON在网络之间传输数据.除了轻量级之外,它还具有结构性.以后将更易于解析和存储.
Use JSON for transporting data to and from the network. Aside from being lightweight, it's also structured. It will be easier to parse and store later on.
操作方法:PHP具有json_encode函数,可将数组转换为JSON字符串.我认为您的框架具有类似的功能.在页面上回显字符串,然后使用JSON.parse
将JSON字符串转换为JS对象. JSON方法来自现代浏览器,但是如果您需要迎合旧版浏览器,请 Crockford拥有一个库进行解析
How to: PHP has a json_encode function to convert arrays into JSON strings. I assume your framework has a similar feature. Have the string echoed on a page then use JSON.parse
to convert from JSON string to a JS object. JSON methods come native in modern browsers but if you need to cater old browsers, Crockford has a library to parse it
使用众所周知的存储框架
Use a well known storage framework
如果需要持久存储来跨页面缓存,我最近遇到了 PersistJS 它将localStorage抽象为浏览器上可用的存储.另外,这是 LZW的JS实现.方便使用,因为localstorage使用字符串存储数据,并且限制为5-10MB.
If a persistent storage is needed for cache across page, I recently came across PersistJS which abstracts localStorage to ones available on the browser. Also, here's a JS implementation of LZW. Keep it handy since localstorage use strings to store data and it has a 5-10MB limit.
方法:使用JSON.stringify
将数据转换为字符串并将其存储在PersistJS中.然后进行检索,获取字符串并使用JSON.parse()
How to: convert the data into a string using JSON.stringify
and store it with PersistJS. Then for retrieval, get the string and parse it back using JSON.parse()
仅在需要时致电
只有在修改,添加或不存在某些内容的情况下,缓存系统才调用服务器.如果有数据,为什么要为此服务器调用?
Have the cache system only call the server if something is modified, added or if something isn't there. If the data is there, why should you call the server for it?
同步缓存
如果您担心陈旧的数据,请使用本Wiki中有关彗星.
If you fear of stale data, then have some AJAX sync your cache system by using some method of live data fetching as described in this wiki about Comet.
最后两点取决于您的缓存框架.但是 BackboneJS 允许它的模型和集合同步到服务器,它们具有我提到的相同功能.
The last two points depend on your cache framework. But BackboneJS allows it's models and collections to sync to the server, which have the same functionality I mentioned.
这篇关于在客户端缓存大量的json结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!