Automatically refresh ASP.NET Output Cache on expiry

99封情书 提交于 2019-12-01 11:11:56

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);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!