From cfab2fd191221a574ec196c3f194ed7aa5a7ca0f Mon Sep 17 00:00:00 2001 From: farhatahmad <35435341+farhatahmad@users.noreply.github.com> Date: Thu, 22 Aug 2019 14:09:39 -0400 Subject: [PATCH] Switched to Redis cache in production (#761) --- Gemfile | 4 ++++ Gemfile.lock | 4 ++++ config/environments/production.rb | 17 ++++++++++++++++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index c07cf100..71b42764 100644 --- a/Gemfile +++ b/Gemfile @@ -83,6 +83,10 @@ group :production do # For a better logging library in production gem "lograge" + + # Use for the cache store in production + gem 'redis' + gem 'hiredis' end # Ruby linting. diff --git a/Gemfile.lock b/Gemfile.lock index 5702d289..8d0a4a4c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -140,6 +140,7 @@ GEM hashie (3.6.0) health_check (3.0.0) railties (>= 5.0) + hiredis (0.6.3) http_accept_language (2.1.1) i18n (1.6.0) concurrent-ruby (~> 1.0) @@ -257,6 +258,7 @@ GEM recaptcha (5.0.0) json redcarpet (3.4.0) + redis (4.1.2) remote_syslog_logger (1.0.4) syslog_protocol request_store (1.4.1) @@ -363,6 +365,7 @@ DEPENDENCIES faker font-awesome-sass (~> 5.9.0) health_check + hiredis http_accept_language i18n-language-mapping (~> 0.1.0) jbuilder (~> 2.5) @@ -385,6 +388,7 @@ DEPENDENCIES random_password recaptcha redcarpet + redis remote_syslog_logger rspec-rails (~> 3.7) rubocop diff --git a/config/environments/production.rb b/config/environments/production.rb index 6a3bc4ac..fa2c6ecf 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -16,7 +16,22 @@ Rails.application.configure do config.consider_all_requests_local = false config.action_controller.perform_caching = true - config.cache_store = :memory_store + if ENV['REDIS_URL'].present? + # Set up Redis cache store + config.cache_store = :redis_cache_store, { url: ENV['REDIS_URL'], + + connect_timeout: 30, # Defaults to 20 seconds + read_timeout: 0.2, # Defaults to 1 second + write_timeout: 0.2, # Defaults to 1 second + reconnect_attempts: 1, # Defaults to 0 + + error_handler: lambda { |method:, returning:, exception:| + config.logger.warn "Support: Redis cache action #{method} failed and returned '#{returning}': #{exception}" + } } + else + config.cache_store = :memory_store + end + config.public_file_server.headers = { 'Cache-Control' => "public, max-age=#{1.years.to_i}" }