Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have used ASP.NET Web API CacheOutput Library for my asp.net project for web API and it working fine, but have another controller from where I have a POST method and I would like to invalidate my cache from that controller.

[AutoInvalidateCacheOutput]
public class EmployeeApiController : ApiController
{ 
    [CacheOutput(ClientTimeSpan = 100, ServerTimeSpan = 100)]
    public IEnumerable<DropDown> GetData()
    {
        //Code here
    }
}


public class EmployeesController : BaseController
{
    [HttpPost]
    public ActionResult CreateEmployee (EmployeeEntity empInfo)
    {
        //Code Here
    }
}

I would like to invalidate Employees Cache when there is add\update in employee controller.

share|improve this question
    
I am not sure but [NoCache] attribute can help. –  Abhilab Das Dec 11 '14 at 6:58
    
I want Cache, but just would like to invalidate when there is change in employee controller –  Suresh Dec 11 '14 at 8:06

1 Answer 1

up vote 3 down vote accepted
+50

It is little tricky, but you can get it in this way:

1. On your WebApiConfig:

// Registering the IApiOutputCache.    
var cacheConfig = config.CacheOutputConfiguration();
cacheConfig.RegisterCacheOutputProvider(() => new MemoryCacheDefault());

We will need of it to get the IApiOutputCache from GlobalConfiguration.Configuration.Properties, if we let the default properties' setup happen the property with the IApiOutputCache won't exists on MVC BaseController request.

2. Create a WebApiCacheHelper class:

using System;
using System.Web.Http;
using WebApi.OutputCache.Core.Cache;
using WebApi.OutputCache.V2;

namespace MideaCarrier.Bss.WebApi.Controllers
{
    public static class WebApiCacheHelper
    {
        public static void InvalidateCache(string controllerName, string actionName)
        {
            var config = GlobalConfiguration.Configuration;

            // Gets the cache key.
            var outputConfig = config.CacheOutputConfiguration();
            var cacheKey = outputConfig.MakeBaseCachekey(controllerName, actionName);

            // Remove from cache.
            var cache = (config.Properties[typeof(IApiOutputCache)] as Func<IApiOutputCache>)();
            cache.Remove(cacheKey);
        }
    }
}

3. Then, call it from your EmployeesController.CreateEmployee action:

public class EmployeesController : BaseController
{
    [HttpPost]
    public ActionResult CreateEmployee (EmployeeEntity empInfo)
    {
        // your action code Here.
        WebApiCacheHelper.InvalidateCache("EmployeeApi", "GetData");
    }
}
share|improve this answer
    
Thanks, but i am using ASP.Net 4.0 and WebApi.OutputCache.V2 is available only in ASP.net 4.5 do you have any other suggestion for work with ASP.Net 4.0? or i have to upgrade my solution to asp.net 4.5 –  Suresh Dec 11 '14 at 13:12
    
I don't know, I've never used WebApi.OutputCache with ASP .NET 4.0. –  giacomelli Dec 11 '14 at 13:22
    
Seems your solution will work for 4.5 as it doesn't solved my problem but accepting as answer hope that will help to others. –  Suresh Dec 15 '14 at 17:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.