加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 在同一用户的请求之间存储状态

发布时间:2020-12-17 03:31:29 所属栏目:百科 来源:网络整理
导读:我知道状态可以存储在cookie和session []哈希中. Which of the following can be used to store state ACROSS requests from the same user? Select all that apply. Cookies The session[] hash Instance variables set by a controller method Class varia
我知道状态可以存储在cookie和session []哈希中.

Which of the following can be used to store state ACROSS requests from the same user? Select all that apply.

  1. Cookies
  2. The session[] hash
  3. Instance variables set by a controller method
  4. Class variables set by a controller method

这个测验问题的答案是(1)和(2).我的问题是:为什么(3)和/或(4)不能跨同一用户的请求存储状态?

解决方法

(3)不能跨请求存储状态.每次请求进入您的Rails应用程序时,都会创建一个控制器实例,并在其上调用相应的操作方法.处理完请求后,控制器实例将被丢弃,并为下一个请求创建一个新实例.

(4)可以在技术上跨请求存储状态,但无论哪个用户发出请求,数据都将在控制器的所有实例之间共享.它也无法从其他控制器访问,如果重新启动服务器就会丢失,并且如果Rails应用程序的多个实例同时运行(在生产方案中很常见),则不会在它们之间共享类变量的内容.

详细说明为什么这类事情是个坏主意:https://stackoverflow.com/a/1029798/742690

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读