Redis字符串化数组 VS 字符串化结构体数组

huangapple go评论69阅读模式
英文:

Redis stringified array VS array of stringified structs

问题

我需要在golang中使用redis作为缓存,并在其中存储一个结构体数组。由于redis只允许存储字符串数组(如果我理解错了,请纠正我),我将对数组中的项进行编组。我想知道我应该使用redis列表,在其中以字符串格式存储编组的结构体,还是可以将整个数组进行编组,并将其作为键值对存储在redis中而不使用列表。使用列表的一个好处是我可以从列表中获取范围内的项,但在这里规模不是问题,因为我将在列表中存储少于100个项。还有什么其他方面需要考虑的吗?

谢谢!

英文:

I need to use redis in golang as cache and store a array of structs in it. Since redis allows only storing array of strings(correct me if I'm wrong), I'll marshal the items in my array. Was wondering shall I use redis list where I'm storing marshaled structs in string format OR I can just marshal the whole array and store as key<>value in redis and not use list. <br>One pro of using list is I can fetch ranged items from the list but scale is not the problem here since I'll be storing less than 100 items in the list. What else should I consider here.

Thankyou!!

答案1

得分: 0

答案取决于您如何使用Redis。

例如,使用JSON(或任何一种序列化方式)存储一个结构体并将其存储在单个位置中,读取/写入起来很容易。

但是,如果您需要高效地检索/更新一个字段,您可以以不同的方式保存它。然而,这种情况非常罕见且复杂难处理。

例如,您需要确保始终按相同的顺序写入,以计算正确的偏移量。如果您需要添加一个新字段,将非常难以实现100%向后兼容。您可能需要创建一个新类型(例如版本2)。

英文:

The answer depends of how you want to use redis

For instance, store one struct using json (or any kind of serialization) and store it in a single position is easy to read / write.

But if you need to efficiently retrieve/ update one field, you can save it in a different way. However this scenario is pretty rare and complex to handle.

For instance you need to be sure you write always in the same order, to calculate the right offset. If you need to add a new field, will be really difficult to be 100% backward compatible. You probably need to create a new type (like a version 2).

huangapple
  • 本文由 发表于 2022年4月10日 05:07:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/71811921.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定