当我们将 redis 与 rails/ruby 一起使用时,Nest 和 redis-namespace gems 之间的平均区别是什么 [英] what is the mean difference between Nest and redis-namespace gems when we use redis with rails/ruby

查看:51
本文介绍了当我们将 redis 与 rails/ruby 一起使用时,Nest 和 redis-namespace gems 之间的平均区别是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两个流行的 gem 用于向 redis 添加命名空间:redis-namespaceNest,如果我真的理解当我们使用相同的 redis 实例服务器时我们需要命名空间对于不同的项目,如果我是对的,这意味着:如果我有 project-1 和 project-2 并且这些项目中的每一个都使用我的本地 redis 存储,那么也许这两个项目有一个 users 键代表我的应用程序的用户,因此为了防止冲突,我需要使用类似项目名称的名称命名 users key :

There is two popular gem for adding namespace to redis : redis-namespace and Nest, if i really understand we need namespace when we use the same instance server of redis with different projects, if i'm right this means : if i have project-1 and project-2 and each of these projects use my local redis storage, then perhaps the two projects have a users key which represente users of my app, so to prevent the conflict i need to namespace users key with something like the name of project :

对于项目 1:

project-1:users

对于项目 1

project-2:users

如果我上面的理解没有错,我们可以使用redis-namespace gem来解决这个问题:

if my above understand is not wrong, we can use redis-namespace gem to solve this like this :

r = Redis::Namespace.new(:project-1, :redis => @r)
r['users']['joe']['email'] = 'joe@example.com'

对于第二个项目(project-2)只需要在实例化新的Redis::Namespace时将project-1改为project-2:

and for the second project (project-2) just need to change project-1 to project-2 when instantiate new Redis::Namespace :

 r = Redis::Namespace.new(:project-2, :redis => @r)
 r['users']['joe']['email'] = 'joe@example.com'

请告诉我,如果我在上述所有解释中没有错!

我们现在可以继续使用 Nest 了:

we can now continue with Nest :

来自文档,我们有这个例子:

from documentation we have this example :

Nest 通过提供已连接到 Redis 的可链接命名空间来帮助您生成密钥:

Nest helps you generate keys by providing chainable namespaces that are already connected to Redis:

>> event = Nest.new("event")
>> event[3][:attendees].sadd("Albert")
>> event[3][:attendees].smembers
=> ["Albert"]

但在这里我不确定 Nest 是否可以帮助我们做与 redis-namespace 相同的事情,或者只是帮助我们生成一个可链接的密钥???

but here i'm not sure if Nest help us to do the same thing as redis-namespace or help us just to generate a chainable keys ???

redis-namespace 和 Nest 之间到底有什么区别?

what is exactly the difference between redis-namespace and Nest ?

推荐答案

免责声明:我是 Nest 的作者.

Disclaimer: I'm the author of Nest.

您可以使用这两个库完成相同的工作,我认为这些工具之间的主要区别在于它们的内部复杂性.虽然 Nest 仅帮助您使用平面键表示结构,但 Redis::Namespace 为每个命令都有一个转换表,因此它更脆弱且 CPU 密集型.

You can accomplish the same with both libraries, and I think the main differentiator between those tools is their internal complexity. While Nest only helps you in representing structure with flat keys, Redis::Namespace has a translation table for every command and it's thus more fragile and CPU intensive.

看看这两个工具的源代码就明白我的意思了:

Take a look at the source code of both tools to see what I mean:

https://github.com/soveran/nest/blob/master/lib/nest.rb

https://github.com/resque/redis-namespace/blob/master/lib/redis/namespace.rb

也就是说,您描述的问题的正确解决方案是为不同的项目使用单独的 Redis 实例.考虑到键空间只是需要考虑的一个方面(这也可以通过 Redis 提供的不同 DB 轻松解决).其他方面(持久性策略、连接和内存限制、关键驱逐等)通常会根据项目的性质进行微调.

That said, the right solution to the problem you described is to have separate Redis instances for different projects. Consider that the keyspace is only one aspect to take into account (which could be also solved easily with the different DBs provided by Redis). The other aspects (persistence strategy, connection and memory limits, key evictions, etc.) are usually fine tuned depending on the nature of the project.

另一个重要的事实是 Redis 是单线程的,许多应用程序都在使用它,您必须为所有请求共享同一个核心.考虑到这一点,拥有不同的 Redis 实例有助于并行化工作.

Another important fact is that Redis is single threaded, and many applications are using it you are subject to sharing the same core for all the requests. With that in mind, having different Redis instances helps you parallelize work.

这篇关于当我们将 redis 与 rails/ruby 一起使用时,Nest 和 redis-namespace gems 之间的平均区别是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆