Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Mar 01, 2012 05:30 PM by benaw
Feb 23, 2012 01:35 PM|LINK
What is the suggested way of working with caching on ASP.NET Web API?
Feb 23, 2012 02:00 PM|LINK
Currently the Web API does not support the MVC caching mechanism, so you need to make your own. I guess you could create an attribute based one with a custom action filter.
I think they are planning to support this before release though?
Feb 23, 2012 02:05 PM|LINK
AFAIK they will indeed work in the caching.
You can add headers to the response like Siggi says, with the use of an action filter. I've got some code for that I you want it.
Another thing you can do is get in the request early to see if there are any headers that like 'If-Modified-Since' and return a 304 with nothing in the body.
Feb 23, 2012 02:07 PM|LINK
Yeah sorry I forgot to ask :) Did you mean HTTP client caching (adding headers) or caching on the server?
Feb 23, 2012 02:15 PM|LINK
thanks! yes, the code would be better even if I can shape the structure in my mind.
Feb 23, 2012 02:16 PM|LINK
Thanks! I meant server side caching.
Feb 23, 2012 03:35 PM|LINK
use at own risk ;-)
Feb 23, 2012 03:39 PM|LINK
And now I see you're talking about server side caching... I cache things in my repository actually. In the ctor I load some things from database a put them in local variables.
Mar 01, 2012 05:30 PM|LINK
There are different layers on the server side, but you might consider Azure's AppFabric's caching service also. It depends on what you want, and whether it is considered output too. (This works over tcp and with ssl, so it can be reached from technically
anywhere. It is in-memory and distributed for speed and HA)
Worth a thought about HTTP ETag machanism from web api too. There was a channel 9 video on the topic too, last year but may need some update to the new version.