我已经在我的Web项目中使用了ASP.NET web库,它可以正常工作,但是有另一个控制器,其中我有POST方法,我想从那个控制器中使缓存失效。
[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
}
}当员工控制器中有add\update时,我想使员工缓存失效。
发布于 2014-12-11 10:42:31
这有点棘手,但您可以通过以下方式获得:
1.在您的WebApiConfig:上
// Registering the IApiOutputCache.
var cacheConfig = config.CacheOutputConfiguration();
cacheConfig.RegisterCacheOutputProvider(() => new MemoryCacheDefault());我们需要它从IApiOutputCache获得GlobalConfiguration.Configuration.Properties,如果我们允许默认属性的设置发生,则IApiOutputCache的属性将不存在于MVC BaseController请求中。
2.创建一个WebApiCacheHelper类:
using System;
using System.Linq.Expressions;
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<T, U>(Expression<Func<T, U>> expression)
{
var config = GlobalConfiguration.Configuration;
// Gets the cache key.
var outputConfig = config.CacheOutputConfiguration();
var cacheKey = outputConfig.MakeBaseCachekey(expression);
// Remove from cache.
var cache = (config.Properties[typeof(IApiOutputCache)] as Func<IApiOutputCache>)();
cache.RemoveStartsWith(cacheKey);
}
}
}然后,从您的操作调用它:
public class EmployeesController : BaseController
{
[HttpPost]
public ActionResult CreateEmployee (EmployeeEntity empInfo)
{
// your action code Here.
WebApiCacheHelper.InvalidateCache((EmployeeApiController t) => t.GetData());
}
}https://stackoverflow.com/questions/27354680
复制相似问题