我想要一个只加载一次的配置模块。
我的代码是
module Configuration
extend self
def settings
@settings ||= # some code that loads the settings
end
end
class Someclass
include Configuration
def evaluate
settings.get_some_setting
end
end
我希望 @settings
被评估并设置一次,但在运行 Rails 服务器时,我看到 @settings
在对服务器的不同 API 调用中重新创建。
我假设要么我没有正确使用变量,要么在每次 API 调用时都实例化了模块(可能是由于不同的线程)。
让 @settings
只计算一次的正确方法是什么?