Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use caching in ASP.NET Web API?

I am using ASP.NET MVC 4 with WEB API

I have the following action, in the action shown below, my service method makes a db call to DoMagic() method and returns an integer value which I am then using on every page, this action is called using an ajax call.

Below is my WEB API action :

[OutputCache(Duration = 86400, VaryByParam = "none")] [ActionName("GetMyMagicNumber")] public int GetMyMagicNumber() {     if (WebSecurity.IsAuthenticated)     {         var revenue = _magicService.DoMagic();         return revenue;     }     return 0; } 

My question : I haved tried using [OutputCache(Duration = 86400, VaryByParam = "none")] and I excepted that only the first time the db call will be made and next subsequent request to this action will return me the cached value, but this is not happening.

A db call is again made, the db call takes time how do I get this working ?

like image 376
Yasser Shaikh Avatar asked Feb 11 '13 12:02

Yasser Shaikh


People also ask

Is caching possible in Web API?

Caching is very common to make applications performant and scalable. If a result is already computed by the application, it is cached in a store so that next time when the same request comes, cached result can be fetched instead of processing the request again.

How does Web API cache work?

Caching is a technique of storing frequently used data or information in a local memory, for a certain time period. So, next time, when the client requests the same information, instead of retrieving the information from the database, it will give the information from the local memory.

How caching is done in ASP.NET explain with example?

Caching is a technique of storing frequently used data/information in memory, so that, when the same data/information is needed next time, it could be directly retrieved from the memory instead of being generated by the application.


2 Answers

Unfortunately, caching is not built into ASP.NET Web API.

Check this out to get you on track: http://www.strathweb.com/2012/05/output-caching-in-asp-net-web-api/

An updated resource here: https://github.com/filipw/AspNetWebApi-OutputCache

EDIT: As of 2020-02-03, even though this answer is quite old, it's still valid.

Both of the URL's above lead to the same project, ASP.NET Web API CacheOutput by Filip W

like image 139
OakNinja Avatar answered Sep 28 '22 01:09

OakNinja


Add a reference to System.Runtime.Caching in your project. Add a helper class :

using System; using System.Runtime.Caching;   public static class MemoryCacher {     public static object GetValue(string key)     {         MemoryCache memoryCache = MemoryCache.Default;         return memoryCache.Get(key);     }      public static bool Add(string key, object value, DateTimeOffset absExpiration)     {         MemoryCache memoryCache = MemoryCache.Default;         return memoryCache.Add(key, value, absExpiration);     }      public static  void Delete(string key)     {         MemoryCache memoryCache = MemoryCache.Default;         if (memoryCache.Contains(key))         {             memoryCache.Remove(key);         }     } } 

Then from your code get or set objects in the cache :

list = (List <ChapterEx>)MemoryCacher.GetValue("CacheItem1"); 

and

MemoryCacher.Add("CacheItem1", list, DateTimeOffset.UtcNow.AddYears(1)); 
like image 27
Sagi Avatar answered Sep 27 '22 23:09

Sagi