1

I have an ImageController class that is called to retrieve an image from the database. The result is stored in the cache through use of the attribute [OutputCache].

Here is the controller:

[OutputCache(Duration=3600,VaryByParam="name")]
public ActionResult Show(string name)
{
   var imageByte = retrieveByteFromDatabase(name);
   if(imageByte != null && imageByte.Length > 0) 
   {
       return File(imageByte, "image/png");
   }
}

Then inside the view, it is being accessed like this:

<img class="image" src="@Url.Action( "show", "image", new { name = "Image.png" })" />

Now, this is an image that is uploaded to the database by a user, and will need to be removed from the cache when another image is uploaded. It is uploaded with this:

[HttpPost]
public ActionResult UploadImage(HttpPostedFileBase file)
{
   uploadToDatabase(file);
   return RedirectToAction("Index", "Home");
}

with the view...

@using (Html.BeginForm("UploadImage", "Image", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
   <input type="file" name="logo" />
   <button class="button" type="submit">Upload</button>
}

Is it possible to remove just this one image from the cache upon uploading a new one? If so - how?

Thanks!

tereško
  • 58,060
  • 25
  • 98
  • 150
wh-dev
  • 537
  • 5
  • 18
  • possible duplicate of [How to invalidate cache data \[OutputCache\] from a Controller?](http://stackoverflow.com/questions/16194140/how-to-invalidate-cache-data-outputcache-from-a-controller) – Tim Rogers May 07 '14 at 16:21
  • using this code flushes everything cached via this function. I just want to be able to single out a specific image and remove it from the flush. This action is used to 'show' several images on the website. – wh-dev May 07 '14 at 16:28
  • 2
    No it doesn't - `RemoveOutputCacheItem` takes a path to a cached item. So provide it with the URL of your image to remove an image. – Tim Rogers May 07 '14 at 16:35
  • Sorry, I must have not been paying attention - thanks! I got it sorted. – wh-dev May 07 '14 at 16:41

0 Answers0