3

As the title says, how can i clear Outputcache on client side? I have several ajax calls that needs to be cleared after user does some specific actions.

I tried:

Response.RemoveOutputCacheItem(Url.Action("Action", "Controller"));

But it didn't work.

I even tried to expire it mannually (even though this would be a bad approach):

                Response.Expires = 0;
                Response.ExpiresAbsolute = DateTime.Now.AddMinutes(-1);
                Response.AddHeader("pragma", "no-cache");
                Response.AddHeader("cache-control", "private");
                Response.CacheControl = "no-cache";

That didn't worked out too.

Just to be clear, i'm using OutputcacheLocation = Client. If i set it to Server the examples above work flawlessly.

AdrianoRR
  • 1,131
  • 1
  • 17
  • 36

2 Answers2

3

If what you need is the axax call to return different data each time despite caching, when called with the same arguments, the only reliable way is to add another variable in the query string, which is always different, e.g. the time down to the millisecond.

Here's how I do it (parameter no_cache):

<script type="text/javascript">
Date.prototype.getTicksUTC = function() 
{
    return Date.parse(this.toUTCString()) + this.getUTCMilliseconds();
} // End Function getTicksUTC


Date.prototype.getTicksGMT = function() 
{
    return Date.parse(this.toGMTString()) + this.getMilliseconds();
} // End Function getTicksGMT

var strURL= "http://localhost/ajax/whateverhandler.ashx?param1=value1&para2=value2&paraN=valueN&no_cache=" + new Date().getTicksUTC();
alert(strURL);

</script> 
Stefan Steiger
  • 78,642
  • 66
  • 377
  • 442
  • but that wouldn't be the same as defining it as NoCache? Is there no way to tell client's browser to clear its cache? – AdrianoRR Apr 11 '12 at 17:14
  • @AdrianoRR: No it wouldn't be the same. First, a client can ignore NoCache. Second, everything else is still cached, just not the ajax call. – Stefan Steiger Apr 13 '12 at 16:31
  • 1
    i understand what you said, but i'd like to clear the cache on client side after some specific actions. From your example, i don't get cache on that ajax call because you're setting an ever-changin timestamp. I believe that's pretty much what $.ajax from jQuery does. I need the cache to be maintained for some time and to be cleared after i tell it to. Anyway, thanks – AdrianoRR Apr 13 '12 at 21:01
  • @AdrianoRR: Then you keep on using the same no_cache argument, until you need a new one. – Stefan Steiger Apr 16 '12 at 11:06
  • now i see what you mean. Whenever i need to update the cache, i just call `Date.prototype.getTicksUTC` and generate another cache free page, right? So, with this i can "control" the client cache, right? I'll try this and if it works, i'll change the answer to yours. – AdrianoRR Apr 16 '12 at 11:55
  • man i must tell this, you're a genius! It worked smoothly! thanks – AdrianoRR Apr 16 '12 at 13:02
  • @StefanSteiger I have similar issue. I need to cache the page all the time, And there is a refresh button, when user hits that I need to neglect the cached data and fetch new one. So with your proposed approach I just need to pass a new parameter with different value so that the request fetches new response, Now my question is the I need to maintain this new data untill the next refresh is hit, That means I will end up in passing the same value for the no_cache parameter right? that means I need to keep track of that parameter untill next refresh is hit. – Rajshekar Reddy Nov 09 '16 at 12:22
  • @StefanSteiger Also as the older cached items are of no use to me I need to make sure I delete them as a performance measure. Need help on this. :) thanks for your time – Rajshekar Reddy Nov 09 '16 at 12:22
-2

You can't. Once you set the cache location to client, you have given the client the responsibility to manage it.

David Savage
  • 760
  • 5
  • 15
  • If so, how would you change cache according to different users and still allow them change data? – AdrianoRR Apr 11 '12 at 14:53
  • 2
    can you provide some reference from where you read that cache on client side cannot be told by a HttpResponse to be cleared? Thanks – AdrianoRR Apr 13 '12 at 21:02