Safety of Thread.current[] usage in rails

后端 未结 4 1901
栀梦
栀梦 2020-12-12 14:21

I keep getting conflicting opinions on the practice of storing information in the Thread.current hash (e.g., the current_user, the current subdomain, etc.). The

4条回答
  •  鱼传尺愫
    2020-12-12 14:42

    The accepted answer is technically accurate, but as pointed out in the answer gently, and in http://m.onkey.org/thread-safety-for-your-rails not so gently:

    Don't use thread local storage, Thread.current if you don't absolutely have to

    The gem for request_store is another solution (better) but just read the readme there for more reasons to stay away from thread local storage.

    There is almost always a better way.

提交回复
热议问题