Asp.net RemoveOutputCacheItem不工作
我有一个Asp.net RemoveOutputCacheItem不工作,asp.net,asp.net-mvc,outputcache,Asp.net,Asp.net Mvc,Outputcache,我有一个ActionResult返回文件结果: [OutputCache(VaryByParam = "document_id;size", Duration = 3 * 60 * 60, Location = OutputCacheLocation.Server)] public ActionResult GetDocumentThumbnail(Guid document_id, int size) { byte[] thumbnail = null; switch (siz
ActionResult
返回文件结果:
[OutputCache(VaryByParam = "document_id;size", Duration = 3 * 60 * 60, Location = OutputCacheLocation.Server)]
public ActionResult GetDocumentThumbnail(Guid document_id, int size)
{
byte[] thumbnail = null;
switch (size)
{
case 100:
thumbnail =
(from a in _unitOfWork.Documents
where a.Id == document_id
select a.Thumbnails.Thumbnail_100).First();
break;
case 25:
thumbnail =
(from a in _unitOfWork.Documents
where a.Id == document_id
select a.Thumbnails.Thumbnail_25).First();
break;
}
return File(thumbnail, "image/png");
}
操作被正确缓存,因此在第一次加载之后,所有其他请求不再进入操作体
当我尝试删除特定文档的缓存时,问题开始出现:
我调用了这个函数,但什么也不做(当我再次请求它时,文档缩略图仍然被缓存)
我也尝试过,但没有结果:我添加了一个自定义路由,因此路径不包含查询字符串参数。不起作用
routes.MapRoute(
name: "DocumentThumbnail",
url: "DocumentThumbnail/{document_id}/{size}",
defaults: new { controller = "Home", action = "GetDocumentThumbnail" }
);
我做错了什么?RemoveOutputCacheItem必须是完整的相对URL。 只有当它应该是
/DocumentThumbnail/{document\u Id}/{code>时,您才传入/DocumentThumbnail/{document\u Id}/{size}
private void RemoveDocumentThumbnailCache(Guid document_Id)
{
foreach(var size in new[] { 100, 25 }) {
var url = Url.Action("GetDocumentThumbnail", new { document_id = document_id, size = size });
HttpResponse.RemoveOutputCacheItem(url);
}
}
这可能有助于了解,希望如此helps@Ehsan:DonutCaching
withFileResult
actions@Zaki:我希望我可以使用Asp.net OutputCache
属性,而不必创建/使用其他内容。它本该起作用的?!!DonutCache适用于特殊情况,您可以使用OutputCache进行常规使用此作品!但是不是很灵活,因为我需要知道其他参数的所有值才能删除条目。是否有任何方法可以通过仅指定开始键来删除具有的缓存项?我已经尝试过/document缩略图/documentIdValue/*
,但没有尝试过,而且似乎不可能。与大多数缓存一样,您只能通过密钥(在本例中为确切的URL路径)访问缓存条目。我可以想出三个备选方案:1)使用VaryByCustom,这样您就可以控制是使用现有缓存还是创建新缓存。问题是您没有清除旧的缓存项。2) 创建自定义OutputCacheProvider。3) 更改URL,使size
位于查询字符串中,而不是URL路径/document缩略图/{document\u Id}?size={size}
。
private void RemoveDocumentThumbnailCache(Guid document_Id)
{
foreach(var size in new[] { 100, 25 }) {
var url = Url.Action("GetDocumentThumbnail", new { document_id = document_id, size = size });
HttpResponse.RemoveOutputCacheItem(url);
}
}