我刚刚实现了我的Azure Web API控制器的输出缓存,使用StrathWeb's library连接到StackExchange.Redis library,通过连接到一个Azure托管的Redis。
我已经编写了一个自定义类,它实现了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类上的所有必需方法: Contains、Set和Get。但是,即使找到并返回缓存的副本,GetExpensiveData()方法也会始终运行并返回“新的”数据。
不会抛出错误。缓存似乎起作用了。然而,我昂贵的代码总是被调用。
感谢您的帮助:)。
发布于 2014-10-28 20:56:07
问题解决了。我错误地从我的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框架从未报告过错误。它只是默默地失败了,并回退到控制器方法。
https://stackoverflow.com/questions/26603114
复制相似问题