为什么要避免在 rails 中使用类变量 @@? [英] Why should we avoid using class variables @@ in rails?

查看:41
本文介绍了为什么要避免在 rails 中使用类变量 @@?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么要避免在 rails 中使用类变量@@?是否有任何安全漏洞.请回答,因为我是 Rails 新手.我经常使用实例变量 @variable_name .我试过一次 @@variable_name .

Why should we avoid using class variables @@ in rails? Is there any security loopholes with that. Please answer as I am new with rails. and I am much using instance variable @variable_name . I tried once @@variable_name .

我只知道类变量是,类变量在对象之间是可共享的

但我真的很想知道为什么我们应该避免在 rails 中使用类变量@@?

推荐答案

仅仅是因为它们不是线程安全的.许多支持 rails=capable 的服务器是多线程的.这意味着在任何给定时间,您的应用程序可能有多个正在运行的实例,并且您的一个用户的任何请求都将被任意分配给其中一个.类变量在进程之间不共享,因此您的类变量在后续请求中可能会有所不同.

Simply because they are not thread safe. Many rails=capable servers are multi-threaded. That means there may be multiple running instances of your application at any given time and any request by one of your users is going to be arbitrarily assigned to one of them. Class variables are not shared between processes so there is a possibility that your class variable will be different in a subsequent request.

即使您有意设法在单线程服务器中运行您的应用,也无法保证您的应用不会在请求之间重新启动,从而丢失您的类变量.

Even if you deliberately manage to run your app in a single threaded server, there is no guarantee that your app won't be restarted between requests, losing your class variable.

如果您想要类似于类变量提供的功能,我强烈建议您查看键值存储,例如 Memcached 或 Redis.

If you want functionality similar to what class variables provide, I strongly recommend that you look into key-value stores such as Memcached or Redis.

这篇关于为什么要避免在 rails 中使用类变量 @@?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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