在网页API缓存搜索结果 [英] Caching search result in Web API

查看:124
本文介绍了在网页API缓存搜索结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我缓存获取方式中的WebAPI用的 strathweb ,现在我想用同样的缓存的输出在我的另一种方法的WebAPI的搜索。所以如何访问缓存获取导致搜索方法?如何找到缓存键到另一个使用它方法?

  [CacheOutput(ClientTimeSpan = 300,ServerTimeSpan = 300)]
    公共IEnumerable的<电影及GT;得到()
    {
        返回repository.GetEmployees()排序依据(C => c.MovieId)。
    }


解决方案

而不是使用的OutputCache ,你可以考虑使用的 的MemoryCache 在内存中存储你的结果更快的访问。

您可以在缓存中存储的结果(取从下面的例子:的 http://www.allinsight.de/caching-objects-in-net-with-memorycache/

  //获取默认的MemoryCache在内存中缓存对象
私人ObjectCache缓存; = MemoryCache.Default;
私人CacheItemPolicy政策;公共ControllerConstructor()
{
    缓存= MemoryCache.Default;    政策=新CacheItemPolicy();
    policy.AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(30);
}公共IEnumerable的<电影及GT; GetMovieBySearchParameter(字符串searchstr)
{
    如果(cache.Get(searchstr)!= NULL)
    {
        返回cache.Get(searchstr)为IEnumerable<电影取代;
    }    //执行搜索,并得到结果。
    IEnumerable的<电影及GT;结果= GetMovies(等等......);    //在缓存中存储的结果。
    cache.Add(searchstr,因此,政策);
}

以上是非常粗糙的(我没有VS在我面前现在就试试吧),但希望其核心思想遇到。

http://www.allinsight.de/caching-objects -in净与-的MemoryCache /

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

解决方案

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/

这篇关于在网页API缓存搜索结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆