Asked by:
Caching in Webapi

Question
-
User1510805204 posted
I want to load cache when my web api application start. So in web api should i use memorycache or normal web cache can be used?
Wednesday, March 15, 2017 7:42 PM
All replies
-
User765422875 posted
Microsoft recommends using MemoryCache.
"When you develop new applications, we recommend that you use the MemoryCache class."
More here:
https://msdn.microsoft.com/en-us/library/dd997357.aspx
And here is another forum question about caching that I answered:
Wednesday, March 15, 2017 8:26 PM -
User1510805204 posted
Thanks for the reply. Suppose i have a web api which is in a distributed server. And if i have the memorycache mechanism implement in the application_start of the web api. Is that enough? or is there a Microsoft built in cache mechanism to control this?
Wednesday, March 15, 2017 9:46 PM -
User-2057865890 posted
Hi Manishev85,
You could use Microsoft class for this called MemoryCache which resides in System.Runtime.Caching dll or different ways(e.g. a library like CacheCow ) to implement cache for Web API.
reference:
https://code.msdn.microsoft.com/Caching-In-Web-API-cb40be30
https://damienbod.com/2014/05/18/exploring-web-api-2-caching/
http://blog.developers.ba/simple-way-implement-caching-asp-net-web-api/
Best Regards,
Chris
Thursday, March 16, 2017 5:14 AM -
User765422875 posted
See my answer below. If you are using a single server, then you can set up your cache in the application_start. Microsoft does not provide a mechanism to control this. Its up to you on how to implement it.
Otherwise, its not a good option for a distributed cache.
Thursday, March 16, 2017 2:29 PM -
User765422875 posted
See my answer below. If you are using a single server, then you can set up your cache in the application_start. Microsoft does not provide a mechanism to control this. Its up to you on how to implement it.
Otherwise, its not a good option for a distributed cache.
Thursday, March 16, 2017 2:47 PM -
User1510805204 posted
Thanks for the reply.How will it behave if the hosted web api is in distributed environment? Does Microsoft has any caching out of the box to handle distributed environment or doing cache in application_start is enough? I will be placing static data which rarely changes in cache.
Thursday, March 16, 2017 2:52 PM -
User765422875 posted
MemoryCache is not a good choice if you have a distributed architecture as you will not be able to easily synchronize your caches. If you do have a distributed architecture and require something robust - then I recommend Redis along with the .NET client developed by stackexchange.
If you have single web server then System.Runtime.Caching or the MemoryCache are sufficient.
Thursday, March 16, 2017 3:08 PM -
User1510805204 posted
Is there any caching for distributed architecture out of box from Microsoft, if yes then any examples? Our client is little skeptic of not using 3rd part tools.
Thursday, March 16, 2017 4:21 PM -
User765422875 posted
Yes and no. It used to be AppFabric, but Microsoft has or will be ending support for it soon. If you want Microsoft support, then you will have to use Azure's redis provider. But redis can also be hosted on prem.
You should tell your client that Redis is probably the #1 distributed caching solution and is supported both in Azure and AWS. Another option is NCache.
I'm not sure I can give you any clearer guidance on this. Hopefully, I've answered all your questions.
Thursday, March 16, 2017 4:49 PM