Question

I have an ActionResult that returns a file result:

[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");
}

The Action gets cached properly, so after the first load, all the other requests doesn't goes into the Action body anymore.

The problem starts when i try to remove the cache for a specific document:

I have this function which i call but does nothing (the document thumbnail is still cached when i request it again)

private void RemoveDocumentThumbnailCache(Guid document_Id)
{
    HttpResponse.RemoveOutputCacheItem("/DocumentThumbnail/" + document_Id + "/");
}

I also tried this solution but with no result: I've added a custom route so the path doesn't contains query string parameters. Not working

routes.MapRoute(
    name: "DocumentThumbnail",
    url: "DocumentThumbnail/{document_id}/{size}",
    defaults: new { controller = "Home", action = "GetDocumentThumbnail" }
);

What i am doing wrong?

Was it helpful?

Solution

RemoveOutputCacheItem must be the full relative URL. You are only passing in /DocumentThumbnail/{document_Id} when it should be /DocumentThumbnail/{document_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);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top