首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CacheOutput属性忽略Azure托管的Redis缓存

CacheOutput属性忽略Azure托管的Redis缓存
EN

Stack Overflow用户
提问于 2014-10-28 15:43:55
回答 1查看 556关注 0票数 1

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

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

下面是一个用法示例:

代码语言:javascript
复制
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类上的所有必需方法: Contains、Set和Get。但是,即使找到并返回缓存的副本,GetExpensiveData()方法也会始终运行并返回“新的”数据。

不会抛出错误。缓存似乎起作用了。然而,我昂贵的代码总是被调用。

感谢您的帮助:)。

EN

回答 1

Stack Overflow用户

发布于 2014-10-28 20:56:07

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

之前..。

代码语言:javascript
复制
public class AzureRedisApiOutputCache : IApiOutputCache
{
  public object Get(string key)
  {
    return AzureRedisCache.Instance.GetDatabase().StringGet(key);
  }
}

之后..。

代码语言:javascript
复制
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框架从未报告过错误。它只是默默地失败了,并回退到控制器方法。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26603114

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档