我正在尝试为 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 示例(它只是使用的工作 atmobject
和不工作的 StackExchange.Redis)
https://github.com/mackayj/WebApi.OutputCache.Redis.ServiceStack
https://github.com/mackayj/WebApi.OutputCache.Redis.StackExchange