c# - StackExchange.Redis 通过 "as byte[]"将 RedisValue 转换为 byte[] 返回 null

标签 c# stackexchange.redis

我正在尝试为 Strathweb.CacheOutput.WebApi2 创建 Redis 提供程序,但尝试从 byte[] -> RedisValue -> byte[] 进行转换时返回 null。

我可以手动将对象类型设置为 byte[] 而不是 var/RedisValue,它会正确地将值作为 byte[] 返回,但是在将其设置为 RedisValue 后无法将其转换为 byte[ ].

他的接口(interface)让 Get 总是返回一个对象,所以我不能在不修改接口(interface)的情况下强制类型或使用单独的调用。

如果我尝试做一个 result as byte[] 我得到 无法通过引用转换、装箱转换、拆箱转换、包装转换或空类型转换将类型“StackExchange.Redis.RedisValue”转换为“byte[]”

如果我尝试做一个 (byte[])result 我得到 Cannot cast 'result' (which has an actual type of 'StackExchange.Redis.RedisValue') to 'byte []'

我是否遗漏了什么,或者我是否必须通过检查它根据 key 查找的数据类型以某种方式破解它?

这是界面:

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

最佳答案

以下使用 StackExchange.Redis 的代码可以设置/获取泛型类型的值并在处理过程中将 RedisValue 转换为 byte[],它应该适用于任何可序列化类型。

    public static void SetItem<T>(string key, T value)
    {

        IDatabase redDb = GetDB();
        redDb.StringSet(key, ToByteArray<T>(value));
    }

    public static T GetItem<T>(string key)
    {
        IDatabase redDb = GetDB();
        RedisValue redisResult = redDb.StringGet(key);
        T objResult = FromByteArray<T>(redisResult);
        return objResult;
    }

   public static byte[] ToByteArray<T>(T obj)
   {
        if (obj == null)
            return null;
        BinaryFormatter bf = new BinaryFormatter();
        using (MemoryStream ms = new MemoryStream())
        {
            bf.Serialize(ms, obj);
            return ms.ToArray();
        }
    }

    public static T FromByteArray<T>(byte[] data)
    {
        if (data == null)
            return default(T);
        BinaryFormatter bf = new BinaryFormatter();
        using (MemoryStream ms = new MemoryStream(data))
        {
            object obj = bf.Deserialize(ms);
            return (T)obj;
        }
    }

关于c# - StackExchange.Redis 通过 "as byte[]"将 RedisValue 转换为 byte[] 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26183727/

相关文章:

c# - 将作为 ICollection<> 导出的 List<> 序列化为 XML

c# - 在派生类中将属性设置为只读

c# - 异步命令和 Task.WhenAny 在 StackExchange.Redis 中等待后出现超时异常

stackexchange.redis - Azure Function App 无法加载 System.IO.Pipelines 连接到 Redis

java - 字符串时间复杂度

c# - RavenDb 无需反序列化即可获取原始 JSON

c# - Javascript 从 C# 方法获取进度

c# - Redis 用 'QUEUED' 回复所有查询

Azure Redis 缓存错误没有可用的连接来服务此操作 :

redis - 使用 StackExchange.Redis 连接到 redis 失败后事务不工作