gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 06:06:25 25 4
gpt4 key购买 nike

我有一个示例,我想将一个对象存储到 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com