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

查看:28
本文介绍了到期时自动刷新 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天全站免登陆