更新时间:2022-10-15 14:57:34
我更新了你的方法来返回类,而不是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;
}