redis - 在 RedisTypedClient (ServiceStack Redis) 中使用复杂类型

标签 redis servicestack

我有一个示例,我想将一个对象存储到 Redis 中。

class CyPoint
    {
        // Fields...
        private bool _Done;
        private string _Color;
        private string _Position;
        private long _Id;

        public long Id
        {
            get { return _Id; }
            set
            {
                _Id = value;
            }
        }

        public string Position
        {
            get { return _Position; }
            set
            {
                _Position = value;
            }
        }

        public string Color
        {
            get { return _Color; }
            set
            {
                _Color = value;
            }
        }

        public bool Done
        {
            get { return _Done; }
            set
            {
                _Done = value;
            }
        }

    }

我正在使用这段代码来存储数据

var redisCyPoint = redis.As<CyPoint>();
            var cpt = new CyPoint
            {
                Id = redisCyPoint.GetNextSequence(),
                Position = "new Vector3(200, 300, 0)",
                Color = "new Vector3(.5f, .7f, .3f)",
            };

            redisCyPoint.Store(cpt);

这在我存储字符串时有效。但是当我将位置和颜色更改为 Vector3(即:float、float、float)时,它只保存 0。 Store 似乎不适用于复杂类型。这是一个限制还是有办法做到这一点?

最佳答案

结构是 serialized as a single scalar string valueToString() 返回。您可以实现 custom support for Structs通过实现构造函数 Vector3(string) 可以从其 ToString() 值填充自身,或实现静态 ParseJson(string) 方法。

否则您可以指定自定义序列化程序来处理序列化,例如:

JsConfig<Vector3>.SerializeFn = v => "{0},{1},{2}".Fmt(v.X,v.Y,v.Z);
JsConfig<Vector3>.DeSerializeFn = s => {
    var parts = s.Split(',');
    return new Vector3(parts[0],parts[1],parts[2]);
};

关于redis - 在 RedisTypedClient (ServiceStack Redis) 中使用复杂类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37976321/

相关文章:

nginx - 使用 SSE 的乘客(服务器发送的事件)

node.js - 如何使用redis发布/订阅

c# - 在保存到 redis 之前压缩对象

asp.net - 检测重复http请求的最有效方法

Laravel Socket.io,Redis 事件正在广播但未在客户端显示

php - 自定义 socket.io 的断开连接事件

redis - 使用 Jredisearch 创建索引时添加前缀

c# - ServiceStack 自动查询 - 如何忽略类中的属性

c# - 如何返回 JSON 而不是关联数组

servicestack - ServiceStack MiniProfiler 能否显示 SQL 参数值,而不仅仅是绑定(bind)的参数名称?