Ruby on Rails - 全局变量? [英] Ruby on Rails - Global Variable?

查看:130
本文介绍了Ruby on Rails - 全局变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名新的Ruby on Rails用户,并有一个问题。我知道我想让我的用户数据库看起来像什么,但想知道我是否应该为其添加额外的值。基本上我需要一个变量来向所有用户发出信号,告诉他们进行某种操作是安全的。这个变量在所有用户中都是持久的,并且应该对所有用户都可见,但是我希望服务器也能够更改这个变量。当使用其他语言进行编程时,我会使用全局变量,所以我想检查这里是否也是这种情况。如果是这样,这是否是实现它的最佳方法: Ruby on Rails中的站点范围全局变量。另外,我将如何更新全局变量。感谢您的帮助!

I am a new Ruby on Rails user and had a question. 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. Also, how would I update the global variables. Thanks for any help!

推荐答案

全局变量不适合您的需求。它并不遍布所有的Ruby进程。如果您的Web服务器产生5个Ruby进程来同时处理5个请求,那么第一个进程中定义的变量将不会被其他进程看到。

A global variable doesn't fit your need. It doesn't spread across all the Ruby processes. 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.

其他解决方案。您可以使用数据库并将标志/信息存储在数据库中。否则,您可以使用文件并将该值存储在文件中。
最好的解决方案是内存共享数据源,例如memcached或Redis。

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.

这篇关于Ruby on Rails - 全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆