gpt4 book ai didi

ruby-on-rails - 在 Ruby/Rails 中使用全局变量或常量变量?

转载 作者:数据小太阳 更新时间:2023-10-29 06:31:23 24 4
gpt4 key购买 nike

假设我们有一个连接到 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com