0

I'm stuck trying to figure out why the outputCache is not getting cleared on the following setup.

Pretty much every link I have checked to clear an outputCacheItem says to just call the RemoveOutputCacheItem(string) method. However I am finding that this is not the case. I get the method once in the debugger, it goes to the ClearCacheName(), Runs the removal. After that I never hit the method again, telling me the cache is stuck and consequently the resultset is null forever.

        [OutputCache(Duration = int.MaxValue, Location = OutputCacheLocation.Server)]
    public JsonResult GetNames()
    {
        if (SomeResultSet != null)
        {
            var results =
                SomeResultSet
                    .Where(x => x.LookupName != null)
                    .OrderBy(x => x.LookupName)
                    .Select(x => new
                    {
                        value = x.LookupName,
                        label = x.LookupName

                    }).Distinct();

            return new JsonResult() {Data = results, JsonRequestBehavior = JsonRequestBehavior.AllowGet};
        }
        ClearCacheNames();
        return null;
    }

    private void ClearCacheNames()
    {
      //  OutputCacheAttribute.ChildActionCache = new MemoryCache(new Guid().ToString());
       Response.RemoveOutputCacheItem(Url.Action("GetNames","Search",null));
    }
macm
  • 544
  • 7
  • 21

1 Answers1

0

The problem is that what the method is caching is the output. After the method clears the cache, the output is generated and cached. So you're doing nothing.

To achieve what you want to do you shouldn't cache the output, but uses System.Web.Caching.Cache inside the action, and cache the data.

You could also try to do it with a filter, but you must create a filter that runs after the OutputCache filter, and provide it the necessary information to clear the cache. It's harder to implement.

JotaBe
  • 38,030
  • 8
  • 98
  • 117
  • What about creating a custom cache attribute where in the OnExecuted method it would then call the ClearCacheNames() method? Like specifically creating a "ClearCache" attribute? – ganders Oct 31 '14 at 18:50