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?
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.
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.
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.
tugberk_ugur...
Participant
1944 Points
1344 Posts
MVP
What is the suggested way of working with caching
Feb 23, 2012 01:35 PM|LINK
What is the suggested way of working with caching on ASP.NET Web API?
tweets as @tourismgeek
SiggiGG
Member
265 Points
105 Posts
Re: What is the suggested way of working with caching
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?
inmykingdom
Member
105 Points
34 Posts
Re: What is the suggested way of working with caching
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.
SiggiGG
Member
265 Points
105 Posts
Re: What is the suggested way of working with caching
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?
tugberk_ugur...
Participant
1944 Points
1344 Posts
MVP
Re: What is the suggested way of working with caching
Feb 23, 2012 02:15 PM|LINK
@inmykingdom
thanks! yes, the code would be better even if I can shape the structure in my mind.
tweets as @tourismgeek
tugberk_ugur...
Participant
1944 Points
1344 Posts
MVP
Re: What is the suggested way of working with caching
Feb 23, 2012 02:16 PM|LINK
@SiggiGG
Thanks! I meant server side caching.
tweets as @tourismgeek
inmykingdom
Member
105 Points
34 Posts
Re: What is the suggested way of working with caching
Feb 23, 2012 03:35 PM|LINK
https://gist.github.com/1893621
use at own risk ;-)
inmykingdom
Member
105 Points
34 Posts
Re: What is the suggested way of working with caching
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.
benaw
Member
211 Points
95 Posts
Re: What is the suggested way of working with caching
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.
http://channel9.msdn.com/Shows/AppFabric-tv/AppFabrictv-Automatic-ETag-Management-with-WCF-Web-API-Message-Handlers