【问题标题】:Using CacheCow to Cache Based On Parameter使用CacheCow根据参数进行缓存
【发布时间】:2017-05-16 21:59:09
【问题描述】:

我的控制器中有一个如下所示的 webapi 端点:

[HttpGet]
    public IHttpActionResult GetPerson(
      string term = null,
      string workspace = null)
    {
        try
        {
            logger.Info("AvPerson start: " + DateTime.Now);
            if (term == null)
            {
                return BadRequest();
            }

            ICoreAVData api = MvcApplication.Container.Resolve<ICoreAVData>();

            List<Person> persons = new List<Person>();
            persons.AddRange(api.GetAllPersonsForTerm(term, workspace));

            if (persons == null)
            {
                return NotFound();
            }

            return Ok(persons);
        }
        catch (Exception)
        {
            return InternalServerError();
        }
    }

term 参数可以不断变化,但workspace 参数显示与用户相关的内容。用户不会离开自己的工作区,因此从用户的角度来看,该参数将保持不变。

我想知道是否可以根据工作空间参数进行 CacheCow 缓存。 IE。如果工作区 1 则缓存它,如果工作区 2 则单独缓存它。

我认识到我必须添加某种逻辑来使该工作区特定的缓存无效。我不是在问这个,因为我相信我知道我该怎么做。我想知道我是否可以为每个工作区参数设置一个单独的缓存条目。

这是我为此控制器设置的路由:

config.Routes.MapHttpRoute(
          name: "avperson",
          routeTemplate: "api/v1/avperson/{action}/{id}",
          defaults: new { controller = "avperson", id = RouteParameter.Optional }

       );

有什么想法吗?

【问题讨论】:

    标签: asp.net-web-api2 cachecow


    【解决方案1】:

    所以解决办法是改变路由。

    config.Routes.MapHttpRoute(
          name: "avperson",
          routeTemplate: "api/v1/{workspace}/avperson/{action}/{id}",
          defaults: new { controller = "avperson", workspace = "all", id = RouteParameter.Optional }
    );
    

    这样做将为每个工作区创建一个单独的缓存条目,然后可以根据需要对其进行验证或无效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-31
      • 1970-01-01
      • 2017-02-25
      • 2015-04-04
      • 2015-01-10
      相关资源
      最近更新 更多