gpt4 book ai didi

ruby-on-rails - 将 YAML 文件作为常量加载到 Rails Controller 中是否有效?

转载 作者:行者123 更新时间:2023-12-04 03:41:40 24 4
gpt4 key购买 nike

我有几个大型数组需要可用于特定 View 。目前我将这些存储在 YAML 文件中并加载到 Controller 常量中,如下所示。

我假设在环境设置期间 Rails 加载文件时,这个常量存储在内存中,但我的偏执部分想知道每次访问 Controller 时我是否都在访问文件系统。任何人都可以建议这方面的最佳做法吗?

class OnboardingController < ApplicationController

BRANDS = YAML.load(File.open("#{Rails.root}/config/brands.yml", 'r'))
STORES = YAML.load(File.open("#{Rails.root}/config/stores.yml", 'r'))

# ...

最佳答案

I assume that this constant is stored in memory when Rails loads the file during environment setup



是的,当文件被加载/需要时,里面的所有东西都会被执行
并分配。因此它只加载一次。

but the paranoid part of me wonders if I'm hitting the filesystem each time that controller is accessed.



部分正确,在开发模式下,每个常量都未设置
请求,但这在生产中应该无关紧要。

Can anyone suggest best practices in this area?



保持原样,缓存只会将解析转发到第一个请求
而不是在你有时间的启动时,因为老 worker
仍在运行。

关于ruby-on-rails - 将 YAML 文件作为常量加载到 Rails Controller 中是否有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9825970/

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