【发布时间】:2014-10-03 17:23:46
【问题描述】:
我正在尝试为 Strathweb.CacheOutput.WebApi2 创建 Redis 提供程序,但尝试从 byte[] -> RedisValue -> byte[] 转换返回 null。
我可以手动将对象类型设置为 byte[] 而不是 var / RedisValue,它会正确地将值返回为 byte[],但是在将其设置为 RedisValue 之后,它无法将其转换为 byte[ ].
他的接口有 Get 总是返回一个对象,所以我不能强制类型或使用单独的调用,而不必修改接口。
如果我尝试做一个result as byte[] 我得到
Cannot convert type 'StackExchange.Redis.RedisValue' to 'byte[]' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion
如果我尝试执行(byte[])result,我会得到Cannot cast 'result' (which has an actual type of 'StackExchange.Redis.RedisValue') to 'byte[]'
我是否遗漏了某些东西,或者我将不得不通过根据密钥检查它要查找的数据类型来以某种方式破解它?
界面如下:
namespace WebApi.OutputCache.Core.Cache
{
public interface IApiOutputCache
{
void RemoveStartsWith(string key);
T Get<T>(string key) where T : class;
object Get(string key);
void Remove(string key);
bool Contains(string key);
void Add(string key, object o, DateTimeOffset expiration, string dependsOnKey = null);
IEnumerable<string> AllKeys { get; }
}
}
这就是它的名称:
var val = _webApiCache.Get(cachekey) as byte[];
if (val == null) return;
编辑:添加我使用 ServiceStack.Redis v3 实现的 API 示例(工作 atm,因为它只使用 object 和 StackExchange.Redis 不起作用)
https://github.com/mackayj/WebApi.OutputCache.Redis.ServiceStack
https://github.com/mackayj/WebApi.OutputCache.Redis.StackExchange
【问题讨论】: