ASP.NET缓存异步 [英] ASP.NET Caching Asynchronously

查看:175
本文介绍了ASP.NET缓存异步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我想我应该链接到这篇文章其中pretty多完成了我的东西。

First off I think I should link to this article which pretty much accomplishes what I what.

下面是我的问题:
我有我的网站上,这将需要缓存一些数据,至​​少15分钟,然后从数据库中再次提取数据的用户控件。问题是拉约需7-10秒从DB拉的结果。

Here's my problem: I had a user control on my site that will need to cache some data for at least 15 minutes and then pull the data again from the DB. The problem is the pull takes about 7-10 seconds to pull the result from the DB.

我的想法是,我可以设置高速缓存喜欢两个小时,然后在缓存的对象属性说当对象被加载(我们称之为LoadDate属性)。然后,我将有code将缓存的对象。

My thought is that I can set the cache to like two hours, then have a property in the cached object to say when the object was loaded (let's call this the LoadDate property). I would then have the code pull the cached object.


  • 如果是空的,我没有选择,只能同步提取数据,然后装入我的用户控制

  • 如果它不是空的,我要继续前进,将数据加载到从缓存的对象我的用户控件。然后我会检查LoadDate属性。如果它已经15分钟以上,然后建立一个异步过程以重新加载缓存。

    • 需要有锁定该缓存对象的过程,而它的更新

    • 我需要一个if语句说,如果对象被锁定,然后就忘了更新它。这将是供其他用户随后的页面加载 - 作为第一个用户将已经在更新缓存,我不希望在更新缓存一遍又一遍;它应该只是第一次调用进行更新。记得我已经载入我的用户控制我连做缓存检查之前

    在我联系到之前的文章,答案设置缓存更新完美,但我不相信这是异步的。现在的问题开始与异步使用Page.RegisterAsyncTask这样做。 【问题1】我似乎无法找到这是否会允许异步进程,即使用户离开该页面继续的任何信息?

    In the article I linked to before, the answer set up the cache updating perfectly, but I don't believe it is asynchronous. The question started with doing it asynchronously using the Page.RegisterAsyncTask. [Question 1] I can't seem to find any information on whether this would allow a asynchronous process to continue even if the user left the page?

    【问题2】任何人有关于如何做到这一点是一个好主意?我有一些code,但它已经变得十分漫长,仍然似乎并没有正常工作。

    [Question 2] Anybody have a good idea on how to do this? I have some code, but it has grown extremely long and still doesn't seem to be working correctly.

    推荐答案

    问题1(RegisterAsyncTask)

    很重要的事情要记住:从客户/用户/浏览器的角度来看,这并没有使请求异步的。如果你注册的任务需要30秒即可完成,浏览器仍然会等待30秒以上。 RegisterAsyncTask做的唯一一件事,就是释放你的工作线程回到IIS的异步调用的持续时间。不要误会我的意思 - 这仍然是一个有价值的和重要的技术。然而,对于用户/浏览器使得该特定请求时,它不会对响应时间产生显着影响。

    Very important thing to remember: from the client/user/browser perspective, this does NOT make the request asynchronous. If the Task you're registering takes 30 seconds to complete, the browser will still be waiting for 30+ seconds. The only thing RegisterAsyncTask does, is to free up your worker thread back to IIS for the duration of the asynchronous call. Don't get me wrong - this is still a valuable and important technique. However, for the user/browser making that particular request, it does not have a noticeable impact on response time.

    问题2

    这可能不是你的问题的最佳解决方案,但它的东西我已经在过去使用,可以帮助:当你的您的项目添加到缓存,指定absoluteExpiration,和onRemoveCallback。创建一个函数,它获取新的数据,并把它放入缓存:这是你应该通过为onRemoveCallback的功能。这样,每次你的缓存数据到期时,会出现一个回调将新鲜的数据备份到缓存中;因为回调发生作为缓存过期事件的结果,还没有一个用户请求等待7-10秒花费缓存新鲜的内容

    This may not be the best solution to your problem, but it's something I've used in the past that might help: when you add your item to the cache, specify an absoluteExpiration, and an onRemoveCallback. Create a function which gets fresh data, and puts it into the cache: this is the function you should pass as the onRemoveCallback. That way, every time your cached data expires, a callback occurs to put fresh data back into the cache; because the callback occurs as a result of a cache expiration event, there isn't a user request waiting for the 7-10 seconds it takes to cache fresh data.

    这是不是一个完美的设计。一些注意事项:

    This isn't a perfect design. Some caveats:


    • 如何开始载入缓存?最简单的方法是从的Application_Start函数中调用缓存加载器的功能。

    • 每隔15分钟,将有在那里缓存为空7-10第二窗口。在这段时间内的任何请求都需要去获取数据本身。根据您的系统使用模式,这可能是一个可以接受的小窗口,这过程中会发生只有极少数的请求。

    • 缓存回调不能保证当你的缓存项到期的情况发生precisely。如果系统是极其重负载下时,将触发之前回调和高速缓存重新加载可能有一个延迟。再次,根据系统的使用情况,这可能是一个非的问题,还是显著的关注。

    对不起,我没有给你一个答案防弹(我要盯紧这个线程 - !也许另一个SO'er一样)。但正如我所说,我用了一些成功这种做法,除非您的系统是非常高的负荷,应该有助于解决这一问题。

    Sorry I don't have a bulletproof answer for you (I'm going to keep an eye on this thread - maybe another SO'er does!). But as I said, I've used this approach with some success, and unless your system is extremely high-load, it should help address the question.

    编辑:上述方法的一个轻微的扭曲,基于OP的评论

    您可以缓存一个虚拟的值,并用它纯粹是为了触发refreshCachedData功能。这不是特别优雅,但我以前做过,也是如此。 :)

    You could cache a dummy value, and use it purely to trigger your refreshCachedData function. It's not especially elegant, but I've done it before, as well. :)

    要详细说明:以迈德特,没有过期的关键,并没有onRemoveCallback保持你的实际缓存数据在缓存中。每次缓存的新数据的时间迈德特你也是一个虚拟的值添加到您的缓存:MyDataRebuildTrigger,用15分钟的到期,并与重建实际缓存数据的onRemoveCallback。这样一来,没有7-10秒的差距时,迈德特是空的。

    To elaborate: keep your actual cached data in the cache with a key of "MyData", no expiration, and no onRemoveCallback. Every time you cache fresh data in "MyData" you also add a dummy value to your cache: "MyDataRebuildTrigger", with a 15-minute expiration, and with an onRemoveCallback that rebuilds your actual cached data. That way, there's no 7-10 second gap when "MyData" is empty.

    这篇关于ASP.NET缓存异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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