From cd2eea2c924ef5114b818afa541109c478d0c2dc Mon Sep 17 00:00:00 2001 From: Guillaume DOTT Date: Mon, 7 Jan 2013 14:21:13 +0100 Subject: [PATCH] Use Rails.cache to cache 'safe' value for each request --- .../request_suspicious_ips_protection.rb | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/project_honeypot_rails/request_suspicious_ips_protection.rb b/lib/project_honeypot_rails/request_suspicious_ips_protection.rb index 17e8d83..a189193 100644 --- a/lib/project_honeypot_rails/request_suspicious_ips_protection.rb +++ b/lib/project_honeypot_rails/request_suspicious_ips_protection.rb @@ -11,8 +11,17 @@ module ProjectHoneypotRails protected def verify_ip_address - session[:project_honeypot_safe] ||= ::ProjectHoneypot.lookup(request.remote_ip).safe? - handle_unverified_request if !session[:project_honeypot_safe] + ip_address = request.remote_ip + key = "httpbl_#{ip_address}" + + if Rails.cache.exist?(key) + safe = Rails.cache.read(key) + else + safe = ::ProjectHoneypot.lookup(ip_address).safe? + Rails.cache.write(key, safe, expires_in: 6.hours) + end + + handle_unverified_request unless safe end def handle_unverified_request