Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use ASP.NET application caching in web API?

For example, in a ASP.NET page you would do something like

Cache.Add({...}) and access it via Cache["key"]. In this context, Cache is the System.Web.Caching.Cache object.

Is there anyway to do this type of ASP.NET application level caching in web API controllers?

like image 381
contactmatt Avatar asked Nov 30 '22 01:11

contactmatt


1 Answers

Take a look at the MemoryCache class. From its MSDN documentation:

The MemoryCache class is similar to the ASP.NET Cache class. The MemoryCache class has many properties and methods for accessing the cache that will be familiar to you if you have used the ASP.NET Cache class. The main differences between the Cache and MemoryCache classes are that the MemoryCache class has been changed to make it usable by .NET Framework applications that are not ASP.NET applications.

You can create a new instance of a MemoryCache yourself, or you can use the default AppDomain-wide instance via the MemoryCache.Default static property.

Edit: You'll need to add a reference to System.Runtime.Caching.dll if you wish to use this type.

like image 188
Levi Avatar answered Dec 10 '22 18:12

Levi