A platform for community discussion. Free, open, simple.
FIX: report cached controller and action to loggers
Sam Saffronb9954b53bbaf
diff --git a/lib/middleware/anonymous_cache.rb b/lib/middleware/anonymous_cache.rbindex 5df0a896d39..c64ae6a90de 100644--- a/lib/middleware/anonymous_cache.rb+++ b/lib/middleware/anonymous_cache.rb@@ -134,6 +134,7 @@ module MiddlewareendMIN_TIME_TO_CHECK = 0.05+ ADP = "action_dispatch.request.parameters"def should_force_anonymous?if (queue_time = @env['REQUEST_QUEUE_SECONDS']) && get?@@ -153,10 +154,13 @@ module Middleware!!(!has_auth_cookie? && get? && no_cache_bypass)end- def cached+ def cached(env = {})if body = $redis.get(cache_key_body)if other = $redis.get(cache_key_other)other = JSON.parse(other)+ if req_params = other[1].delete(ADP)+ env[ADP] = req_params+ end[other[0], other[1], [body]]endend@@ -169,7 +173,7 @@ module Middleware# NOTE in an ideal world cache still serves out cached content except for one magic worker# that fills it up, this avoids a herd killing you, we can probably do this using a job or redis tricks# but coordinating this is tricky- def cache(result)+ def cache(result, env = {})status, headers, response = resultif status == 200 && cache_duration@@ -180,6 +184,13 @@ module Middlewareparts << partend+ if req_params = env[ADP]+ headers_stripped[ADP] = {+ "action" => req_params["action"],+ "controller" => req_params["controller"]+ }+ end+$redis.setex(cache_key_body, cache_duration, parts.join)$redis.setex(cache_key_other, cache_duration, [status, headers_stripped].to_json)@@ -218,7 +229,7 @@ module Middlewareresult =if helper.cacheable?- helper.cached || helper.cache(@app.call(env))+ helper.cached(env) || helper.cache(@app.call(env), env)else@app.call(env)enddiff --git a/spec/components/middleware/request_tracker_spec.rb b/spec/components/middleware/request_tracker_spec.rbindex 35f9921f3c8..ca0f613db84 100644--- a/spec/components/middleware/request_tracker_spec.rb+++ b/spec/components/middleware/request_tracker_spec.rb@@ -277,12 +277,24 @@ describe Middleware::RequestTracker dotracker = Middleware::RequestTracker.new(cache)uri = "/path?#{SecureRandom.hex}"- tracker.call(env("REQUEST_URI" => uri, "ANON_CACHE_DURATION" => 60))++ request_params = {+ "a" => "b",+ "action" => "bob",+ "controller" => "jane"+ }++ tracker.call(env("REQUEST_URI" => uri, "ANON_CACHE_DURATION" => 60, "action_dispatch.request.parameters" => request_params))expect(@data[:cache]).to eq("store")tracker.call(env("REQUEST_URI" => uri, "ANON_CACHE_DURATION" => 60))expect(@data[:cache]).to eq("true")++ # not whitelisted+ request_params.delete("a")++ expect(@env["action_dispatch.request.parameters"]).to eq(request_params)endit "can correctly log detailed data" do