到期时自动刷新ASP.NET输出缓存 [英] Automatically refresh ASP.NET Output Cache on expiry

查看:101
本文介绍了到期时自动刷新ASP.NET输出缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这样的ASP.NET输出缓存来缓存一些昂贵的页面,

I have a few expensive pages that I cache using ASP.NET output cache like so,

[OutputCache(Duration=3600, VaryByParam = "none")]

很明显,缓存将在3600秒(1小时)后过期,并且碰巧加载该页面的下一个可怜的家伙将不得不等待从dabatase刷新缓存.

Obviously, the cache will expire after 3600 seconds (1 hour), and the next poor guy that happens to load that page will have to wait for the cache to be refreshed from the dabatase.

我的问题是,如何使缓存在到期后立即刷新?这样,当缓存刚刚过期时,下一个碰巧访问该页面的人将不必等待缓存刷新,而是获得了新的缓存吗?

My question is, how do I make the cache to be refreshed immediately on expiry? So that the next guy who happens to visit the page when the cache had just expired will not have to wait for the cache to be refreshed and instead is served with a new cache?

更新:我需要非常频繁地更新缓存(1小时至3个小时),因为我也不希望数据过时.

Update: I need the cache to be updated pretty frequently (1 hour to 3 hour) as I do not want the data to stale for too long either.

推荐答案

我认为,只需使用OutputCache即可实现所需的功能.

I don's think, that you can achieve what you need using just OutputCache.

基本上,您需要数据存储和辅助程序.对于存储,您可以使用从静态变量到外部数据库的任何内容.

Basically you need data storage and worker. For storage you can using anything from static variable to external database.

与工人相同.这可能只是简单的长期运行任务或外部服务.基本示例,因此您可以了解我在说什么

Same thing with worker. It's might be just simple long running task or external service. Basic sample, so you can get the idea of what i am talking about

public class TestController : Controller
{
    private static int _result = 0;


    static TestController()
    {
        Task.Factory.StartNew(async () =>
        {
            while (true)
            {
                await Task.Delay(new TimeSpan(0, 0, 5));
                _result++;
            }

        }, TaskCreationOptions.LongRunning);
    }

    public ActionResult Index()
    {
        return Json(_result, JsonRequestBehavior.AllowGet);
    }
}

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

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