假设我们有一个连接到 memcache 或 redis...哪种风格是首选,为什么?
MEMCACHE = Memcache.new(...)
REDIS = Redis.new(...)
或
$memcache = Memcache.new(...)
$redis = Redis.new(...)
最佳答案
您可能想使用 Redis.current
更多信息 here
例如,在初始化器中:
Redis.current = Redis.new(host: 'localhost', port: 6379)
然后在你的其他类(class)中:
def stars
redis.smembers("stars")
end
private
def redis
Redis.current
end
关于ruby-on-rails - 在 Ruby/Rails 中使用全局变量或常量变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10900855/