我有一个实例MemoryCache类。 我需要为缓存中的所有对象设置过期时间。 有人可以帮忙吗?
private static List<UserRole> _userRoles
{
get
{
var lstUserRole = (List<UserRole>)MemoryCache.Default.Get("UserRoles");
if (lstUserRole == null)
{
var _svcFactory = DependencyResolver.Current.GetService<QueryFactory>();
lstUserRole = _svcFactory.GetQuery<UserRole>().
Include(x => x.Role.Rights).
Where(x => x.UserId == _userId && !x.DeletedOn.HasValue && !x.Role.DeletedOn.HasValue).ToList();
foreach(var userRole in lstUserRole)
{
userRole.Role.Rights = userRole.Role.Rights.Where(x => !x.DeletedOn.HasValue).ToList();
}
MemoryCache.Default.Add("UserRoles", lstUserRole, DateTime.Now.AddMinutes(RolesCacheMinutes));
}
return lstUserRole;
}
}