在网页API内存高速缓存 [英] Memory Cache in web api
问题描述
我在我的网页API寻找缓存在那里我可以使用由一个API方法的输出(在12小时变化一次)的subsequesnt呼叫,然后我发现的这个解决方案,以便在的,但我有理解困难,并根据以下code
私人的IEnumerable< TEntity> GetFromCache< TEntity>(字符串键,Func键<&IEnumerable的LT; TEntity>> valueFactory)其中TEntity:类
{
ObjectCache缓存= MemoryCache.Default;
VAR为newValue =新懒人<&IEnumerable的LT; TEntity>>(valueFactory);
CacheItemPolicy政策=新CacheItemPolicy {AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(30)};
//下面将返回现有项目或行,如果它不存在,增加了新的价值
VAR值= cache.AddOrGetExisting(键,为newValue,政策)懒惰和LT; IEnumerable的< TEntity>取代;
收益率(值?newValue)以.value的; //懒< T>处理锁定本身
}
我不知道如何调用,并在下面情况下使用这种方法吗?
我有一个方法来获取
公开的IEnumerable<员工>得到()
{
返回repository.GetEmployees()排序依据(C => c.EmpId)。
}
和我想缓存获取和我的其他方式使用GetEmployeeById(输出)或搜索()
公共电影GetEmployeeById(INT EMPID)
{
//在缓存搜索员工获取
} 公共IEnumerable的<员工> GetEmployeeBySearchString(字符串searchstr)
{
//在缓存搜索获取
}
我更新了你的方法来返回类,而不是IEnumberable:
私人TEntity GetFromCache< TEntity>(字符串键,Func键< TEntity> valueFactory)其中TEntity:类
{
ObjectCache缓存= MemoryCache.Default;
//懒类提供懒initializtion将eavaluate仅如果该项目不在缓存中存在valueFactory前pression
VAR为newValue =新懒人< TEntity>(valueFactory);
CacheItemPolicy政策=新CacheItemPolicy {AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(30)};
//下面将返回现有项目或行,如果它不存在,增加了新的价值
VAR值= cache.AddOrGetExisting(键,为newValue,政策)懒惰和LT; TEntity取代;
收益率(值?newValue)以.value的; //懒< T>处理锁定本身
}
那么你可以使用这个方法,如:
公共电影GetMovieById(INT movieId)
{
VAR cacheKey =电影+ movieId;
变种电影= GetFromCache&所述;电影及GT;(cacheKey,()=> {
//加载电影从DB
返回context.Movies.First(X => x.Id == movieId);
});
回到电影;
}
和搜索电影
[ActionName(搜索)]
公共IEnumerable的<电影及GT; GetMovieBySearchParameter(字符串searchstr)
{
VAR cacheKey =电影+ searchstr;
VAR电影= GetFromCache<&IEnumerable的LT;电影>>(cacheKey,()=> {
返回repository.GetMovies()排序依据(C => c.MovieId)。.ToList();
});
回归电影;
}
I was looking for Caching in my web api where i can use output of one api method(that changes once in 12hrs) for subsequesnt calls and then i found this solution on SO,but i am having a difficulty in understanding and using the below code
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
}
I am not sure how to call and use this method in below context? I have a method Get
public IEnumerable<Employee> Get()
{
return repository.GetEmployees().OrderBy(c => c.EmpId);
}
and i want to cache the output of Get and use it in my other methods GetEmployeeById() or Search()
public Movie GetEmployeeById(int EmpId)
{
//Search Employee in Cached Get
}
public IEnumerable<Employee> GetEmployeeBySearchString(string searchstr)
{
//Search in Cached Get
}
I updated your method to return classes instead of 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
}
then you can use this method like:
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;
}
and to search movies
[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;
}
这篇关于在网页API内存高速缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!