【问题标题】:CacheOutput Attribute Ignoring Azure-hosted Redis CacheCacheOutput 属性忽略 Azure 托管的 Redis 缓存
【发布时间】:2014-10-28 07:43:55
【问题描述】:

我刚刚使用 StrathWeb's library 连接到 StackExchange.Redis library 连接到 Azure 托管的 Redis 缓存,实现了我的 Asp.Net Web API 控制器的输出缓存。

我编写了一个自定义类,它实现 StrathWeb IApiOutputCache interface 并调用等效的 StackExchange 方法。这在 Global.asax.cs 中注册为缓存输出提供者。

这是一个使用示例:

public class MyApiController : ApiController
{
  private const int FIFTEEN_MINUTES_IN_SECONDS = 900;

  [CacheOutput(ClientTimeSpan = FIFTEEN_MINUTES_IN_SECONDS, ServerTimeSpan = FIFTEEN_MINUTES_IN_SECONDS)]
  async public Task<Data> GetAsync(int param1, string param2)
  {
    return await GetExpensiveData();
  }

  [Serializable]
  public class Data
  {
    // Members omitted for brevity
  }
}

当调用 api 端点时,我可以看到框架正确调用了我的 IApiOutputCache 类上的所有必需方法:包含、设置和获取。但是,即使找到并返回缓存副本,GetExpensiveData() 方法也会始终运行并返回“新”数据。

不会抛出任何错误。缓存似乎正在工作。然而,我的昂贵代码总是被调用。

感谢您的帮助:)。

【问题讨论】:

  • 你能分享你的 IApiOutputCache Redis 实现吗?

标签: caching azure asp.net-web-api outputcache stackexchange.redis


【解决方案1】:

问题解决了。我从 IApiOutputCache 类错误地调用了 Redis。

之前...

public class AzureRedisApiOutputCache : IApiOutputCache
{
  public object Get(string key)
  {
    return AzureRedisCache.Instance.GetDatabase().StringGet(key);
  }
}

之后……

public class AzureRedisApiOutputCache : IApiOutputCache
{
  public object Get(string key)
  {
    // Call the extension method that also performs deserialization...
    return AzureRedisCache.Instance.GetDatabase().Get(key);
  }
}

public static class RedisDatabaseExtensions
{
    public static object Get(this IDatabase cache, string key)
    {
        return Deserialize<object>(cache.StringGet(key));
    }
}

这让我困惑了一段时间,因为 CacheOutput 框架从未报告过错误。它只是默默地失败并退回到控制器方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-28
    • 2015-12-08
    • 2020-06-06
    • 1970-01-01
    • 2020-06-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多