Is Rails shared-nothing or can separate requests access the same runtime variables?

后端 未结 4 801
囚心锁ツ
囚心锁ツ 2020-12-05 07:48

PHP runs in a shared-nothing environment, which in this context means that every web request is run in a clean environment. You can not access another request\'s data excep

4条回答
  •  天涯浪人
    2020-12-05 08:12

    Here is a relatively simple example that illustrates what can happen if you are not careful about modifying shared objects.

    1. Create a new Rails project: rails test

    2. Create a new file lib/misc.rb and put in it this:

      class Misc
        @xxx = 'Hello'
        def Misc.contents()
          return @xxx
        end
      end
      
    3. Create a new controller: ruby script/generate controller Posts index
    4. Change app/views/posts/index.html.erb to contain this code:

      <%
        require 'misc'; y = Misc.contents() ; y << ' (goodbye) '
      %>
      
      <%= y %>

      (This is where we modify the implicitly shared object.)

    5. Add RESTful routes to config/routes.rb.
    6. Start the server ruby script/server and load the page /posts several times. You will see the number of ( goodbye) strings increasing by one on each successive page reload.

提交回复
热议问题