简体   繁体   English

javascript和asp.net的缓存问题

[英]Caching issue with javascript and asp.net

I asked a question a while back on here regarding caching data for a calendar/scheduling web app, and got some good responses. 我刚才在这里问了一个有关为日历/计划Web应用程序缓存数据的问题,并得到了一些很好的答复。 However, I have now decided to change my approach and stat caching the data in javascript. 但是,我现在决定更改我的方法,并统计将数据缓存在javascript中。

I am directly caching the HTML for each day's column in the calendar grid inside the $('body').data() object, which gives very fast page load times (almost unnoticable). 我直接在$('body')。data()对象内的日历网格中为每一天的列缓存HTML,这提供了非常快的页面加载时间(几乎不引人注意)。

However, problems start to arise when the user requests data that is not yet in the cache. 但是,当用户请求尚未在缓存中的数据时,就会出现问题。 This data is created by the server using an ajax call, so it's asynchronous, and takes about 0.2s per week's data. 该数据是由服务器使用ajax调用创建的,因此它是异步的,每周大约需要0.2s的数据。

My current approach is simply to block for 0.5s when the user requests information from the server, and cache 4 weeks either side in the inital page load (and 1 extra week per page change request), however I doubt this is the optimal method. 我当前的方法只是在用户从服务器请求信息时阻塞0.5s,并在初始页面加载的每一侧缓存4周(每个页面更改请求额外缓存1周),但是我怀疑这是否是最佳方法。

Does anyone have a suggestion as to how to improve the situation? 有没有人建议如何改善这种情况?

To summarise: 总结一下:

  • Each week takes 0.2s to retrieve from the server, asynchronously. 每周需要0.2秒的时间从服务器异步获取数据。
  • Performance must be as close to real-time as possible. 性能必须尽可能接近实时。 (however the data is not needed to be fully real-time: most appointments are added by the user and so we can re-cache after this) (但是不需要数据是完全实时的:大多数约会是由用户添加的,因此我们可以在此之后重新缓存)
  • Currently 4 weeks are cached on either side of the inial week loaded: this is not enough. 当前在初始周的任一侧都缓存了4周:这还不够。
  • to cache 1 year takes ~ 21s, this is too slow for an initial load. 缓存1年大约需要21秒,对于初始加载来说这太慢了。

As I read your description, I thought of 2 things: Asynchrony and Caching. 在阅读您的描述时,我想到了两件事:异步和缓存。

First, Asynchrony 一,异步

Why would you block for 0.5s? 你为什么要阻挡0.5秒? Why not use an ajax call, and in the callback, update the page with the retrieved info. 为什么不使用ajax调用,并在回调中使用检索到的信息更新页面。 There is no blocking for a set time, it is done asynchronously. 在设定的时间内没有阻塞,它是异步完成的。 You'd have to suppress multiple clicks though, while a request is outstanding, but that shouldn't be a problem at all. 尽管请求悬而未决,但是您必须抑制多次单击,但这根本不是问题。

You can also pre-load the in-page cache in the background, using setInterval or better, setTimeout . 您还可以使用setInterval或更好的setTimeout在后台预加载页内缓存。 Especially makes sense if the cost to compute or generate the calendar is long and the data size is relatively small - in other words, small enough to store months in the in-page cache even if it is never used. 如果计算或生成日历的成本很高并且数据大小相对较小,换句话说,即使它从未使用过,也足够小以至于可以在页内缓存中存储数月,这特别有意义。 Sounds like you may be doing this anyway and only need to block when the user jumps out of the range of cached data. 听起来您无论如何都可以这样做,并且仅在用户跳出缓存数据范围时才需要阻塞。

Intelligent Caching 智能缓存

I am imagining the callback function - the one that is called when the ajax call completes - will check if the currently selected date is on the "edge" of the cached data - either the first week in cache or the last week (or whatever). 我在想象回调函数-ajax调用完成时调用的函数-将检查当前选择的日期是否在缓存数据的“边缘”上-缓存的第一周还是最后一周(或其他) 。 If the user is on the edge, then the callback can send out an additional request to optimistically pre-load the cache up to the 4 week limit, or whatever time range makes sense for your 80% use cases. 如果用户处在边缘状态,则回调可以发出额外的请求,以乐观地预加载高速缓存,直到4周的限制,或在您的80%用例有意义的任何时间范围内。

You may also consider caching the generated calendar data on the server side , on a per-user basis. 您还可以考虑按用户在服务器端缓存生成的日历数据。 If it is CPU- and time-intensive to generate these things, then it should be a good trade to generate once and keep it in the server-side cache, invalidating only when the user makes an update. 如果生成这些东西需要占用大量CPU和时间,那么一次生成并将其保存在服务器端缓存中是一个不错的选择,只有在用户进行更新时才使它无效。 With x64 servers and cheap memory, this is probably very feasible. 使用x64服务器和廉价内存,这可能非常可行。 Depending on the use cases, it may make for a much more usable interaction, the 2nd time a user connects to the app. 根据用例的不同,它可能会使用户第二次连接到应用程序,从而使交互作用更加实用。 You could even consider pre-loading the server-side cache on a per-user basis, before the user requests any calendar. 您甚至可以考虑在用户请求任何日历之前按用户预先加载服务器端缓存。

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

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