开发者

Is a redis instance can only work for 1 project?

开发者 https://www.devze.com 2023-01-06 22:22 出处:网络
I\'m using Rails with redis. From the introduction of Redis, I found such information: start redis server:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消