I'm using Rails with redis.
From the introduction of Redis, I found such information:
start redis server:
redis-server
use redis client:
> redis-cli
redis> set key value
OK
redis> get key
"value"
From the sample, I have a question:
Is a redis instance can only work f开发者_如何学Cor 1 project? You can see, there is no "database" or "collection" or similar things. If two different projects use the same redis, they may change the same key to invalid value.
So, do I need to create different instances with different ports for different Rails projects?
Keep in mind that redis also has databases (16 of them if I remember correctly) – they're just not named, they're numbers. So for example, if you're using redis-rb
to connect, you'll get a snippet like this:
$redis = Redis.new(:host => 'localhost', :port => 6379, :db => 5)
That'll connect to database 5. I use this a lot to run tests as well so my tests don't interfere with my development database.
If sharing a single Redis instance (or cluster) between two or more applications then you should probably namespace your keys to partition them rationally between those applications for the reasons you've observed. Take a look at the redis-namespace gem which provides a nice Ruby interface for doing this.
精彩评论