what is the mean difference between Nest and redis-namespace gems when we use redis with rails/ruby

帅比萌擦擦* 提交于 2020-01-05 05:47:09

问题


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 :

for project-1 :

project-1:users

for project-1

project-2:users

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'

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'

please tell me if i'm not wrong in all this above explanation !

we can now continue with Nest :

from documentation we have this example :

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"]

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 ???

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


回答1:


Disclaimer: I'm the author of Nest.

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

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.

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.




回答2:


You know http://redis.io/commands/select for he parallel running of multiple databases on one server? Here you don't have a key conflict and the databases are separated. Only if you access to keys from all databases at once it's not possible.



来源:https://stackoverflow.com/questions/18559961/what-is-the-mean-difference-between-nest-and-redis-namespace-gems-when-we-use-re

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!