I've a bunch of rails app on my server that should be able to use Redis as cache engine.
Do I've to start one instance of Redis for each of my application, or does the Redis support scoping?
I'm worried that if I remove one value in one app, the value with the same key will be removed for all of my apps.
I do NOT for example want this to happen.
App 1
Rails.cache.write("key", "value")
App 2
Rails.cache.read("key") => "value"
App 3
Rails.cache.delet开发者_开发百科e("key")
App 1
Rails.cache.read("key") => nil
I suggest running a server for every application. Every additional Redis instance only uses 1 megabyte more memory when empty, so the overhead is small and it is ok to run tens of servers in a single instance. Also an idle Redis server is going to use a minimal amount of memory.
So basically by running multiple servers you are not wasting resources, but instead gaining speed as you'll use all your CPUs or CPU cores, given that Redis is single threaded.
A common method for this is one of two things:
- Prefix your keys with some sort of "type" identifier, like
app1:key
,app2:key
. - Use separate DBs for each using
SELECT
. By default, connections start out against DB 0. If you doSELECT 1
for app1,SELECT 2
for app2, etc., you are guaranteed that no other application will clobber that data.
The solution was to use redis-store with the namespace param.
Here is my config/production.rb
file.
# App 1
config.cache_store = :redis_store, {path: "/tmp/redis.sock", db:1, namespace: "app1"}
# App 2
config.cache_store = :redis_store, {path: "/tmp/redis.sock", db:2, namespace: "app2"}
精彩评论