有人可以帮我吗?我有以下代码来存储和修复catch,但是,它不起作用.即使我在slidingExpiration中将其设置为14天,缓存也会在几分钟内到期.提前致谢!
public static List<ReplyDTO> VideoCommentList()
{
List<ReplyDTO> replyList = new List<ReplyDTO>();
if (HttpRuntime.Cache["videoComment"] == null)
{
HttpRuntime.Cache.Remove("videoComment");
HttpRuntime.Cache.Insert("videoComment", replyList, null, Cache.NoAbsoluteExpiration, TimeSpan.FromDays(14));
}
else
{
replyList = (List<ReplyDTO>)HttpRuntime.Cache["videoComment"];
}
if (replyList.Count > 8)
{
replyList = replyList.OrderByDescending(x => x.DateCreated).Take(8).ToList();
}
else
{
replyList = replyList.OrderByDescending(x => x.DateCreated).ToList();
}
return replyList;
}
public static List<ReplyDTO> AddVideoComment(ReplyDTO replyDTO)
{
List<ReplyDTO> replyList = new List<ReplyDTO>();
replyList = VideoCommentList();
replyList.Add(replyDTO);
HttpRuntime.Cache.Insert("videoComment", replyList, null, Cache.NoAbsoluteExpiration, TimeSpan.FromDays(14));
if (replyList.Count > 8)
{
replyList = replyList.OrderByDescending(x => x.DateCreated).Take(8).ToList();
}
else
{
replyList = replyList.OrderByDescending(x => x.DateCreated).ToList();
}
return replyList;
}
Run Code Online (Sandbox Code Playgroud)
ASP.net缓存是内存中的,因此如果您的IIS进程或应用程序池回收,它将变得清晰.您可以检查以下可能导致流程再循环的事项
web.config,IIS将关闭旧实例并慢慢将流量传输到新实例,在此过程中内存将被回收.如何检查:您可以通过检查AppDomain.IsFinalizingForUnload并在回调期间记录该情况来检测此情况.编辑
在您的程序中,您要将项目添加replyList到缓存然后执行.Take()操作.作为replyList参考对象,如果您修改它,它也将在缓存中更新.因此,如果在您的程序中,如果您这样做replyList == null,将更新缓存中的项目.
所以修改你的代码并尝试
public static List<ReplyDTO> VideoCommentList()
{
List<ReplyDTO> replyList = new List<ReplyDTO>();
if (HttpRuntime.Cache["videoComment"] == null)
{
//Call to .Remove is not required
//HttpRuntime.Cache.Remove("videoComment");
HttpRuntime.Cache.Insert("videoComment", replyList, null,
Cache.NoAbsoluteExpiration, TimeSpan.FromDays(14));
}
else
{
//No need to check count > 8, Take will handle it for you
replyList = ((List<ReplyDTO>)HttpRuntime.Cache["videoComment"])
.OrderByDescending(x => x.DateCreated)
.Take(8).ToList();
}
return replyList;
}
public static List<ReplyDTO> AddVideoComment(ReplyDTO replyDTO)
{
//Read from cache
List<ReplyDTO> replyList = ((List<ReplyDTO>)HttpRuntime.Cache["videoComment"]);
if(replyList == null)
replyList = VideoCommentList();
replyList.Add(replyDTO);
HttpRuntime.Cache.Insert("videoComment", replyList, null, Cache.NoAbsoluteExpiration, TimeSpan.FromDays(14));
//Here you are creating a new list, and not referencing the one in the cache
return replyList.OrderByDescending(x => x.DateCreated).Take(8).ToList();
}
Run Code Online (Sandbox Code Playgroud)
重要建议
如果要检查从缓存中删除对象的时间和原因,可以在插入时获取CacheItemRemovedCallback选项的帮助.使用this和CacheItemRemovedReason参数,您可以记录从缓存中删除对象的原因.原因
Insert或Remove方法从缓存中删除了该项.希望这些信息可以帮助您.
| 归档时间: |
|
| 查看次数: |
4312 次 |
| 最近记录: |