【问题标题】:Memory Cache in web apiWeb api中的内存缓存
【发布时间】:2014-10-27 04:10:32
【问题描述】:

我在我的 web api 中寻找Caching,在那里我可以使用一种 api 方法的输出(在 12 小时内更改一次)进行后续调用,然后我在 SO 上找到了this solution,但我遇到了困难理解和使用下面的代码

private IEnumerable<TEntity> GetFromCache<TEntity>(string key, Func<IEnumerable<TEntity>> valueFactory) where TEntity : class 
{
    ObjectCache cache = MemoryCache.Default;
    var newValue = new Lazy<IEnumerable<TEntity>>(valueFactory);            
    CacheItemPolicy policy = new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(30) };
    //The line below returns existing item or adds the new value if it doesn't exist
    var value = cache.AddOrGetExisting(key, newValue, policy) as Lazy<IEnumerable<TEntity>>;
    return (value ?? newValue).Value; // Lazy<T> handles the locking itself
}

我不确定如何在以下上下文中调用和使用此方法? 我有一个方法Get

  public IEnumerable<Employee> Get()
    {
        return repository.GetEmployees().OrderBy(c => c.EmpId);
    }

我想缓存 Get 的输出并在我的其他方法 GetEmployeeById() 或 Search() 中使用它

        public Movie GetEmployeeById(int EmpId)
        {
           //Search Employee in Cached Get
        }

        public IEnumerable<Employee> GetEmployeeBySearchString(string searchstr)
        {
          //Search in Cached Get
        }

【问题讨论】:

    标签: c# asp.net-mvc asp.net-web-api asp.net-web-api2 memorycache


    【解决方案1】:

    我更新了您的方法以返回类而不是 IEnumberable:

    private TEntity GetFromCache<TEntity>(string key, Func<TEntity> valueFactory) where TEntity : class 
    {
        ObjectCache cache = MemoryCache.Default;
        // the lazy class provides lazy initializtion which will eavaluate the valueFactory expression only if the item does not exist in cache
        var newValue = new Lazy<TEntity>(valueFactory);            
        CacheItemPolicy policy = new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(30) };
        //The line below returns existing item or adds the new value if it doesn't exist
        var value = cache.AddOrGetExisting(key, newValue, policy) as Lazy<TEntity>;
        return (value ?? newValue).Value; // Lazy<T> handles the locking itself
    }
    

    那么你可以像这样使用这个方法:

    public Movie GetMovieById(int movieId)
    {
        var cacheKey = "movie" + movieId;
        var movie = GetFromCache<Movie>(cacheKey, () => {       
            // load movie from DB
            return context.Movies.First(x => x.Id == movieId); 
        });
        return movie;
    }
    

    搜索电影

    [ActionName("Search")]
    public IEnumerable<Movie> GetMovieBySearchParameter(string searchstr)
    {
         var cacheKey = "movies" + searchstr;
         var movies = GetFromCache<IEnumerable<Movie>>(cacheKey, () => {               
              return repository.GetMovies().OrderBy(c => c.MovieId).ToList(); 
         });
         return movies;
    }
    

    【讨论】:

    • @little 它是在 GetFromCache 方法中完成的
    • @little 您应该在 valueFactory 函数中调用您的存储库(仅当对象不在缓存中时才会执行)。看看我的回答,我不是在调用存储库,而是直接调用上下文,所以只需将上下文替换为存储库即可。
    • @little 我在示例中更新了 GetMovieBySearchParameter 方法,现在它正在使用存储库。
    • @F11,你好,你想问什么?
    • @F11 是的,给我发一个聊天链接。
    猜你喜欢
    • 1970-01-01
    • 2014-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-05
    • 2013-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多