简体   繁体   English

Ruby on Rails - 全局变量?

[英]Ruby on Rails - Global Variable?

I am a new Ruby on Rails user and had a question. 我是一个新的Ruby on Rails用户并且有一个问题。 I have an idea of what I want my Users DB to look like but was wondering whether or not I should add an additional value to it. 我知道我希望我的用户数据库看起来像什么,但想知道我是否应该为它添加一个额外的值。 Basically I need a variable to signal to all users that it is safe to proceed with a certain action. 基本上我需要一个变量来向所有用户发出信号,表示可以安全地继续执行某个操作。 This variable would be persistent across all users and should be visible to all users, but I want the server to be able to change this variable as well. 此变量将在所有用户中保持不变,并且对所有用户都应该可见,但我希望服务器也能够更改此变量。 When programming in other languages, I would use a global variables, so I wanted to check if that is also the case here. 当用其他语言编程时,我会使用全局变量,所以我想检查这是否也是这种情况。 If so, would this be the best approach for going about it: Site-Wide Global Variables in Ruby on Rails . 如果是这样,这将是解决它的最佳方法: Ruby on Rails中的站点范围全局变量 Also, how would I update the global variables. 另外,我如何更新全局变量。 Thanks for any help! 谢谢你的帮助!

A global variable doesn't fit your need. 全局变量不适合您的需要。 It doesn't spread across all the Ruby processes. 它并没有遍布所有Ruby进程。 If your web server spawns 5 ruby processes to handle 5 request at the same time, the variable defined in the first process won't be visible to the others. 如果您的Web服务器同时生成5个ruby进程来处理5个请求,则第一个进程中定义的变量将对其他进程不可见。

There are other solutions available. 还有其他解决方案。 You can use a database and store the flag/information on the database. 您可以使用数据库并将标志/信息存储在数据库中。 Otherwise, you can use a file and store the value in the file. 否则,您可以使用文件并将值存储在文件中。 The best solution would be an in-memory shared data source, such as memcached or Redis. 最好的解决方案是内存中的共享数据源,例如memcached或Redis。

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

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