is local to that request “ スレッドローカルとは、スレッドごとに独立したデータを持つ仕組み。 そのためスレッド間で共有されることはない。 module RequestStore class Middleware def initialize(app) @app = app end def call(env) RequestStore.begin! status, headers, body = @app.call(env) body = Rack::BodyProxy.new(body) do RequestStore.end! RequestStore.clear! end returned = true [status, headers, body] ensure unless returned RequestStore.end! RequestStore.clear! end end end end ライブラリを読み込むと専用の Rackミドルウェアが差し込まれ、リクエスト終了 時にキャッシュが自動クリアされる module RequestStore def self.store Thread.current[:request_store] ||= {} end