【问题标题】:Caching ASP.NET Web API with CacheCow使用 CacheCow 缓存 ASP.NET Web API
【发布时间】:2014-07-18 07:31:53
【问题描述】:

我正在尝试使用 CacheCow 实现缓存。我有两个问题:

  1. 在某些情况下,我需要手动使某些资源的缓存无效。

    例如,我有一个名为purchase 的资源,而另一个名为pointMovements 的资源。他们没有完全联系,但在purchase 发帖,意味着pointMovement 的一些变化。 Cachecow 没有检测到这些变化,因为我没有调用 pointmovements 的 API。所以当我调用pointmovements的端点时,值被缓存了,我无法获取新值。

    要解决这个问题,我需要手动使之无效,这怎么可能?

  2. 有些控制器我不想缓存。我正在尝试使用属性来做到这一点,但它不起作用。我正在关注this article,但属性被忽略了。

    如何指定要缓存的控制器?

【问题讨论】:

  • 您好,很抱歉没有回复您,您还有问题吗?

标签: c# caching asp.net-web-api cachecow


【解决方案1】:

我遇到了同样的问题,并找到了问题 2 的解决方案(无论默认设置如何都禁用缓存)。

// This forces the server to not provide any caching by refreshing its cache table immediately (0 sec)
[HttpCacheRefreshPolicy(0)]
// This forces the client (browser) to not cache any data returned from the server (even if ETag is present) by setting the time-out to 0 and no-cache to true.
[HttpCacheControlPolicy(true, 0, true)]
public void MyController : ApiControler {... }

这些属性必须一起应用才能正常工作。您还可以通过为每个操作提供相同的规则来控制操作级别的缓存。

我还没有弄清楚问题 1 的解决方案。但是请注意这个空间的更新。

更新 我找到了问题 1 的解决方案。

  1. 在您的 IoC 容器中注册 CachingHandler(在我的例子中是 IUnityContainer
  2. ICachingHandler 注入您的Web API 控制器。
  3. 要使资源无效,请使用ICachingHandler.InvalidateResource(HttpRequestMessage)

请参阅下面的代码示例。该解决方案已经过测试。

public class Bootstrapper
{
    //...

    // Create a new caching handler and register it with the container.
    public void RegisterCache(HttpConfiguration config, IUnityContainer container)
    {
        var cachingHandler = new CachingHandler(config);
        // ...

        container.RegisterInstance<ICachingHandler>(cachingHandler);
    }
}

public class ResourceContoller : ApiController
{
    private ICachingHandler _cachingHandler;

    public ResourceContoller(ICachingHandler cachingHandler)
    {
        _cachingHandler = cachingHandler;       
    }

    [HttpPost]
    public void DeleteResource(int resourceId)
    {
        // Do the delete
        // ...

        // Now invalidate the related resource cache entry      
        // Construct a http request message to the related resource
        // HINT: The "DefaultApi" may not be your api route name, so change this to match your route.
        // GOTCHA: The route matching mechanism is case sensitive, so be aware!
        var relatedResource = new HttpRequestMessage(HttpMethod.Get, Url.Link("DefaultApi", new {controller = "linkedresource", action = "getlinkedresource", id: resourceId}));

        // Invalidate the resource with the caching handler.
        _cachingHandler.InvalidateResource(relatedResource);
    }
}

【讨论】:

    【解决方案2】:

    抱歉回复晚了。

    正如@Tri Q 所说,这样做的方法是使用我在此博客中解释的属性:

    http://byterot.blogspot.co.uk/2013/03/rest-asp-net-wep-api-0.4-new-features-breaking-change-cachecow-server.html

    【讨论】:

      【解决方案3】:

      我使用以下代码解决了您的第一个问题。这里我扩展了IRoutePatternProvider 接口。请记住,您在GetRoutePattern 中返回的内容应该与您在GetLinkedRoutePatterns 中返回的内容相匹配。只有这样添加和删除才会起作用。试试看。

      内部Application_Start

      CachingHandler cacheHandler = new CachingHandler(GlobalConfiguration.Configuration);
              cacheHandler.RoutePatternProvider = new CacheRoutePatternProvider();
              GlobalConfiguration.Configuration.MessageHandlers.Add(cacheHandler);
      

      自定义类

      public class CacheRoutePatternProvider : IRoutePatternProvider
      {
          public string GetRoutePattern(HttpRequestMessage request)
          {
              string path = request.RequestUri.AbsolutePath;
              if (!path.EndsWith("/"))
                  path += "/";
      
              return path;
          }
      
          public IEnumerable<string> GetLinkedRoutePatterns(HttpRequestMessage request)
          {
              string path = request.RequestUri.AbsolutePath;
              if(!path.EndsWith("/"))
                  path += "/";
      
              int segmentIndex;
              // return each segment of the resource heirarchy
              while ((segmentIndex = path.LastIndexOf("/")) > 0)
              {
                  path = path.Substring(0, segmentIndex);
      
                  if(path.Contains("/api/"))
                      yield return path + "/";
              }
      
              yield break;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-26
        • 1970-01-01
        • 2018-02-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多