2

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:

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.

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?

1 Answer 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.

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

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Is the cache URL-sensitive? In other words, can I access the action through localhost or must I know the external URL?
AFAIK urls are not case sensitive under Windows.
I mean, if the website is deployed at http://somedomain.com, can I use a WebClient to load the resources to be cached from http://localhost?
Why do you want to load from localhost if your application is deployed on somedomain.com? The cache is stored in the memory of the web server hosting your web application, so you need to send the HTTP request to this server. If you have configured your IIS server to respond to localhost requests then you could of course send the request to localhost, it will be the same application instance anyway.
The image generation code is shared code used in more than one project. It could self-initialize if it provides an Initialize routine that's called from Application_Start. If the website always listens to localhost in addition to the public URL (which certainly would have to be implemented by convention), the initialization code would not need knowledge of the public URL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.