ASP。当控制器直接调用时,Net MVC OutputCache

时间:2022-06-06 03:51:07

I have a controller action that dynamically renders a graphic, given several input parameters. Since it is slightly noticeable when the graphic renders, I use the Output Cache to avoid re-rendering.

我有一个控制器动作,在给定几个输入参数的情况下,动态呈现图形。由于在图形呈现时稍微明显,所以我使用输出缓存来避免重新呈现。

There are several dozen input parameter combinations that are used very frequently. I thought it would be a good idea to warm the cache so that the first visitor to use a given one of those combinations does not experience a delay.

有几十个输入参数组合经常使用。我认为最好先把缓存预热一下,这样第一个使用这些组合的访问者就不会遇到延迟。

To that end, I directly call the controller from Application_Start() like this:

为此,我直接从Application_Start()调用控制器,如下所示:

UtilController uc = new UtilController();
uc.GenerateImage(p1, p2, p3);

By setting a breakpoint, I see that the controller action is called and the image is generated. However, the first (and only first) time that image is requested by the browser, it is generated again. For purposes of this test, the browser is configured not to cache anything, and I carefully compared the parameters used to call the controller action.

通过设置断点,我看到控制器动作被调用,图像被生成。但是,当浏览器第一次(也是唯一一次)请求该映像时,它将再次生成。对于这个测试,浏览器被配置为不缓存任何内容,我仔细地比较了用于调用controller操作的参数。

Is the Output Cache not invoked when the controller action is called directly? Is there a better method (hopefully one self-contained to the web project) of pre-warming the cache?

当直接调用控制器操作时,是否没有调用输出缓存?是否有更好的方法(希望是web项目的自包含方法)预暖缓存?

1 个解决方案

#1


2  

Is the Output Cache not invoked when the controller action is called directly?

当直接调用控制器操作时,是否没有调用输出缓存?

It is not invoked because you are not going through an HTTP request/response cycle which triggers it. You are directly calling a method that happens to be your action.

它不会被调用,因为您不会经历一个触发它的HTTP请求/响应循环。您直接调用的方法恰好是您的操作。

Is there a better method (hopefully one self-contained to the web project) of pre-warming the cache?

是否有更好的方法(希望是web项目的自包含方法)预暖缓存?

You could send an HTTP request using a WebClient to the given address. That would of course work only if you have configured the cache to be stored on the server. If you configured the location to be downstream or on the client it won't be of any use.

您可以使用WebClient发送一个HTTP请求到给定的地址。当然,只有在您将缓存配置为存储在服务器上时,这才会起作用。如果您将位置配置为下游或客户端,那么它就没有任何用处。

#1


2  

Is the Output Cache not invoked when the controller action is called directly?

当直接调用控制器操作时,是否没有调用输出缓存?

It is not invoked because you are not going through an HTTP request/response cycle which triggers it. You are directly calling a method that happens to be your action.

它不会被调用,因为您不会经历一个触发它的HTTP请求/响应循环。您直接调用的方法恰好是您的操作。

Is there a better method (hopefully one self-contained to the web project) of pre-warming the cache?

是否有更好的方法(希望是web项目的自包含方法)预暖缓存?

You could send an HTTP request using a WebClient to the given address. That would of course work only if you have configured the cache to be stored on the server. If you configured the location to be downstream or on the client it won't be of any use.

您可以使用WebClient发送一个HTTP请求到给定的地址。当然,只有在您将缓存配置为存储在服务器上时,这才会起作用。如果您将位置配置为下游或客户端,那么它就没有任何用处。