Caching search result in Web API

2019-03-04 12:47发布

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);
    }

2条回答
smile是对你的礼貌
2楼-- · 2019-03-04 12:52

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.

[OutputCache(Duration=86400, VaryByParam="searchstr")] // for MVC
[CacheOutput(ClientTimeSpan = 50, ServerTimeSpan = 50)] // for Web API
[ActionName("Search")]
public IEnumerable<Movie> GetMovieBySearchParameter(string searchstr)
{           
}

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.

System.Web.HttpRuntime.Cache.Add(
                key,
                value,
                null,
                expiration,
                Cache.NoSlidingExpiration,
                CacheItemPriority.Normal,
                callback);

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.

查看更多
ら.Afraid
3楼-- · 2019-03-04 13:08

Rather than using the OutputCache, you could consider using MemoryCache 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/

//Get the default MemoryCache to cache objects in memory
private ObjectCache cache; = MemoryCache.Default;
private CacheItemPolicy policy;

public ControllerConstructor()
{
    cache = MemoryCache.Default;

    policy = new CacheItemPolicy();
    policy.AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(30);
}

public IEnumerable<Movie> GetMovieBySearchParameter(string searchstr)
{
    if (cache.Get(searchstr) != null)
    {
        return cache.Get(searchstr) as IEnumerable<Movie>;
    }

    // Do the search and get the results.
    IEnumerable<Movie> result = GetMovies(blah.....);

    // Store the results in cache.
    cache.Add(searchstr, result, policy);
}

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/

查看更多
登录 后发表回答