Redis 中嵌套结构的替代方案? [英] Alternatives to Nested Structures in Redis?
问题描述
我经常遇到这样的情况,我要存储的信息比任何 Redis 的简单数据结构所能容纳的都复杂.我仍然想使用 Redis,但我想知道人们在理想情况下希望使用嵌套结构时是否有任何标准替代方案?
I keep running into cases where I have more complicated information to store than what can fit into any of Redis' simple data structures. I still want to use Redis, but I was wondering if there are any standard alternatives people use when ideally they'd like to use a nested structure?
推荐答案
你基本上有两种策略:
您可以序列化您的复杂对象并将它们存储为字符串.我们建议使用 json 或 msgpack 作为序列化格式.这很容易从大多数客户端语言进行操作.如果需要服务器端访问,那么服务器端 Lua 脚本可以轻松地编码/解码此类对象,因为 Redis 是使用 Lua 的 msgpack 和 json 支持编译的.
you can serialize your complex objects and store them as strings. We suggest json or msgpack for the serialization format. This is easy enough to manipulate from most client-side languages. If server-side access is needed, then a server-side Lua script can easily encode/decode such objects since Redis is compiled with msgpack and json support for Lua.
您可以将对象拆分为不同的键.您可以存储多个键,例如 user:id、user:id:address_list、user:id:document_lists 等,而不是将 user:id 和复杂的数据结构存储到此 id 中……如果需要原子性,则流水线 MULTI/EXEC块可用于保证数据一致性,并聚合往返.
you can split your objects in different keys. Instead of storing user:id and a complex data structure to this id, you can store several keys such as user:id, user:id:address_list, user:id:document_lists, etc ... If you need atomicity, pipelining MULTI/EXEC blocks can be used to guarantee the data consistency, and aggregate the roundtrips.
在这个答案中看到一个简单的例子:
See a simple example in this answer:
最后,Redis 不是面向文档的数据库.如果你真的有很多复杂的文档,也许MongoDB、ArangoDB、CouchDB、Couchbase等解决方案可以更好地为你服务......
Finally, Redis is not a document oriented database. If you really have a lot of complex documents, perhaps you could be better served by solutions such as MongoDB, ArangoDB, CouchDB, Couchbase, etc ...
这篇关于Redis 中嵌套结构的替代方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!