简体   繁体   English

Ruby on Rails全球Cach

[英]Ruby on rails global cach

I need some global variable on my server that will always be there Is there a built in way to use some global cache on a rails server? 我需要在服务器上始终存在一些全局变量,是否有内置的方法可以在Rails服务器上使用某些全局缓存?

I'm looking for a query cache that will last for an hour. 我正在寻找一个持续一个小时的查询缓存。

Is it recommended? 推荐吗?

I always put variables I want to access everywhere into the environments/{production,development,..}.rb files, depending of the environment. 我总是将要访问的变量放到environments/{production,development,..}.rb文件中,具体取决于环境。 If you want some fast datastore, which needs to store some more complex data, I would suggest redis . 如果您需要一些快速的数据存储,需要存储一些更复杂的数据,我建议使用redis If redis is to heavy for your use case you can also use the Rails Caching . 如果redis在您的用例中很繁琐,您也可以使用Rails Caching

you can always add an app specific yaml file that you load from a file in initializers 您始终可以添加从初始化程序中的文件加载的应用特定的Yaml文件

in initializers: 在初始值设定项中:

raw_config = File.read("#{Rails.root}/config/app_config.yml") raw_config = File.read(“#{Rails.root} /config/app_config.yml”)

APP_CONFIG = YAML.load(raw_config)[Rails.env].symbolize_keys APP_CONFIG = YAML.load(raw_config)[Rails.env] .symbolize_keys

then you can access APP_CONFIG from anywhere in your app APP_CONFIG[:some_var] 那么您可以从应用程序APP_CONFIG [:some_var]中的任何位置访问APP_CONFIG

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM