我正在尝试为Strathweb.CacheOutput.WebApi2创建一个Redis提供程序,但是将byte[] -> RedisValue -> byte[]进行转换时返回null。
我可以手动将对象类型设置为byte[]而不是var / RedisValue,这样它将正确地返回字节数组。但是,在将其设置为RedisValue之后,它无法将其转换为byte[]。
他的接口始终返回一个对象,因此我无法强制类型或使用单独的调用而不必修改接口。
如果我尝试执行
这是接口:
我可以手动将对象类型设置为byte[]而不是var / RedisValue,这样它将正确地返回字节数组。但是,在将其设置为RedisValue之后,它无法将其转换为byte[]。
他的接口始终返回一个对象,因此我无法强制类型或使用单独的调用而不必修改接口。
如果我尝试执行
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示例(目前正在工作,因为它只使用object
),以及不起作用的StackExchange.Redis。
https://github.com/mackayj/WebApi.OutputCache.Redis.ServiceStack
https://github.com/mackayj/WebApi.OutputCache.Redis.StackExchange
RedisValue
应该实现IConvertible
接口。这可能会在下一个版本中实现。 - Marc Gravellas string
和as MediaTypeHeaderValue
调用)。我尝试将其设置为dynamic result = _database.GetString(key)
,但结果相同,您所说的动态作弊是什么意思?有没有办法只返回一个对象而不是将其转换为RedisValue? - Johnobject
" 应该是什么?byte[]
?string
?int
? 如果你总是想要byte[]
,那么在使用 "object
" 之前 **只需将其转换为byte[]
**,即RedisValue val = ...; object o = (byte[])val; // <=== use that
。 - Marc Gravell