I am caching Get method in webapi with strathweb,now i want to use same cached output in my another webapi method Search.So how to access cached Get result in Search Method?How to find Cache Key to use it in another methods?
[CacheOutput(ClientTimeSpan = 300, ServerTimeSpan = 300)]
public IEnumerable<Movie> Get()
{
return repository.GetEmployees().OrderBy(c => c.MovieId);
}
The simplest way to go about this would be to add OutputCache attribute to your controller. It is supported only in MVC controllers. For Web API controllers, you can use this - https://github.com/filipw/AspNetWebApi-OutputCache
The below will cache the results per search term for 24 hours. However, this method is naive and works only if the number of search terms are really small. If the number of search terms is large (as would be in this case), it adds enormous memory pressure, which will cause the ASP.NET app pool to recycle, so you will lose the cache.
In your case the whole result set can be cached once and can be updated every 24 hours. You can look at System.Web.HttpRuntime.Cache. It supports an expiration date and a callback function when the item is removed from the cache. You can add the movie list to the cache and then query the cache. Just make sure to refresh/re-populate the cache when the items expire.
I would add a CachedRepository decorator to the repository which you reference in the controller. In that cached repository, I'd try to return data from the cache if it is there. If not I would fetch and return data from the original source and also add it to the cache.
Rather than using the
OutputCache
, you could consider usingMemoryCache
to store your results in memory for faster access.You can store the results in cache (taking example from the following : http://www.allinsight.de/caching-objects-in-net-with-memorycache/
The above is very rough (I don't have VS in front of me right now to try it), but hopefully the core idea comes across.
http://www.allinsight.de/caching-objects-in-net-with-memorycache/