假设我们有一个到memcache或redis的连接。哪种风格是首选的?为什么?
MEMCACHE = Memcache.new(...)
REDIS = Redis.new(...)
或
$memcache = Memcache.new(...)
$redis = Redis.new(...)
发布于 2013-05-10 11:23:09
您可能想要使用Redis.current
More info here
例如,在初始化器中:
Redis.current = Redis.new(host: 'localhost', port: 6379)
然后在你的其他课程中:
def stars
redis.smembers("stars")
end
private
def redis
Redis.current
end
发布于 2012-06-06 00:19:59
它们不是等价的构造。根据您的应用程序,它们可能可以互换,也可能不能互换,但它们在语义上是不同的。
# MEMCACHE is a constant, subject to scoping constraints.
MEMCACHE = Memcache.new(...)
# $memcache is a global variable: declare it anywhere; use it anywhere.
$memcache = Memcache.new(...)
发布于 2012-06-06 00:17:47
我是一个“常量”,因为它传达了它应该是...常量。
全局变量并不意味着它们不应该发生变化。
https://stackoverflow.com/questions/10900855
复制相似问题