B Z
B Z

Reputation: 9463

Remove action from Output Cache

ASP.Net MVC 3.

I found similar questions/answers, but none seem to fix this issue...

Similar: HttpResponse.RemoveOutputCacheItem is not working

How to "invalidate" portions of ASP.NET MVC output cache?

I am using OutputCache to cache a FileContentResult (an image).

My action looks like this:

[HttpGet]
[OutputCache(Location = OutputCacheLocation.Client, Duration = 300, VaryByParam = "id")]
public FileContentResult Photo(int id) {
    byte[] photo = //GetPhoto;

    return File(photo,"image/jpeg");
}

In my view I have to following:

<img src="@Url.Action("Photo", "Client", new {id = Model.Id})"/>

This works fine, and the output caching is working as expected....Now the problem

I am trying to reset the cache after an update.

The code in the action that updates looks like this:

var url = Url.Action("Photo", "Client", new {id = Model.Id});
Response.RemoveOutputCacheItem(url);

The problem is the cache doesn't reset. When I debug, I can't find where the cache object is (I tried System.Web.HttpContext.Current.Cache, but that doesn't seem to have the cached item).

Thanks for any help!

Upvotes: 3

Views: 3799

Answers (2)

Andrey Kuleshov
Andrey Kuleshov

Reputation: 685

I'm not completely sure about your case, but I found about child actions on view: they use their own cache (not System.Web.HttpContext.Current.Cache), which you can access through static member

CacheOutputAttribute.ChildActionCache

Cache of child actions has pretty complicated key, which I was not able to calculate. So, I end up with solution to add a new variable(such a version or timestamp) to my model and pass it as parameter to action method; so I can invalidate my cache with changing this property.

However I'm not 100% sure it is your case, as you are doing no child action caching.

Upvotes: 1

B Z
B Z

Reputation: 9463

I ended up using this code and it works great!

http://antix.co.uk/Blog/IfModifiedAttribute

Upvotes: 3

Related Questions