From 37053239c07e49a5f64ce3607bca81c2b64ba208 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Mon, 3 May 2021 14:25:59 +0100 Subject: [PATCH 1/2] Switch cache store from memcached to redis --- Gemfile | 3 +++ Gemfile.lock | 4 ++++ config/application.yml.example | 2 -- config/environments/development.rb | 9 ++++++++- config/environments/production.rb | 8 +++++--- config/environments/staging.rb | 8 +++++--- 6 files changed, 25 insertions(+), 9 deletions(-) diff --git a/Gemfile b/Gemfile index 8afdfa339b..609730d223 100644 --- a/Gemfile +++ b/Gemfile @@ -82,6 +82,9 @@ gem 'rack-rewrite' gem 'rack-ssl', require: 'rack/ssl' gem 'roadie-rails', '~> 2.2.0' +gem 'redis', '>= 4.0', require: ['redis', 'redis/connection/hiredis'] +gem 'hiredis' + gem 'combine_pdf' gem 'wicked_pdf' gem 'wkhtmltopdf-binary' diff --git a/Gemfile.lock b/Gemfile.lock index 45b3e09784..50e4d3c05b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -285,6 +285,7 @@ GEM tilt hashdiff (1.0.1) highline (2.0.3) + hiredis (0.6.3) i18n (1.8.10) concurrent-ruby (~> 1.0) i18n-js (3.8.2) @@ -447,6 +448,7 @@ GEM rb-inotify (0.10.1) ffi (~> 1.0) redcarpet (3.5.1) + redis (4.2.5) regexp_parser (2.1.1) request_store (1.5.0) rack (>= 1.4) @@ -672,6 +674,7 @@ DEPENDENCIES good_migrations haml highline (= 2.0.3) + hiredis i18n i18n-js (~> 3.8.2) immigrant @@ -706,6 +709,7 @@ DEPENDENCIES rails_safe_tasks (~> 1.0) ransack (= 2.4.1) redcarpet + redis (>= 4.0) responders roadie-rails (~> 2.2.0) roo (~> 2.8.3) diff --git a/config/application.yml.example b/config/application.yml.example index 2b93ead158..1346dae4c2 100644 --- a/config/application.yml.example +++ b/config/application.yml.example @@ -66,5 +66,3 @@ SMTP_PASSWORD: 'f00d' # config/initializers/feature_toggles.rb for details. # # BETA_TESTERS: ofn@example.com,superadmin@example.com - -MEMCACHED_VALUE_MAX_MEGABYTES: '4' diff --git a/config/environments/development.rb b/config/environments/development.rb index 72c6e7d7cb..3b22ee205f 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -11,7 +11,14 @@ Openfoodnetwork::Application.configure do config.cache_classes = !!ENV["PROFILE"] # :file_store is used by default when no cache store is specifically configured. - config.cache_store = :memory_store if !!ENV["PROFILE"] + if !!ENV["PROFILE"] + config.cache_store = :redis_cache_store, { + driver: :hiredis, + url: ENV.fetch("OFN_REDIS_URL", "redis://localhost:6379/0"), + namespace: "cache", + expires_in: 90.minutes + } + end config.eager_load = false diff --git a/config/environments/production.rb b/config/environments/production.rb index 826a4de616..cf90833eb2 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -42,9 +42,11 @@ Openfoodnetwork::Application.configure do config.log_formatter = Logger::Formatter.new.tap { |f| f.datetime_format = "%Y-%m-%d %H:%M:%S" } # Use a different cache store in production - memcached_value_max_megabytes = ENV.fetch("MEMCACHED_VALUE_MAX_MEGABYTES", 1).to_i - memcached_value_max_bytes = memcached_value_max_megabytes * 1024 * 1024 - config.cache_store = :mem_cache_store, { value_max_bytes: memcached_value_max_bytes } + config.cache_store = :redis_cache_store, { + driver: :hiredis, + url: ENV.fetch("OFN_REDIS_URL", "redis://localhost:6379/0"), + namespace: "cache" + } # Enable serving of images, stylesheets, and JavaScripts from an asset server # config.action_controller.asset_host = "http://assets.example.com" diff --git a/config/environments/staging.rb b/config/environments/staging.rb index 826a4de616..cf90833eb2 100644 --- a/config/environments/staging.rb +++ b/config/environments/staging.rb @@ -42,9 +42,11 @@ Openfoodnetwork::Application.configure do config.log_formatter = Logger::Formatter.new.tap { |f| f.datetime_format = "%Y-%m-%d %H:%M:%S" } # Use a different cache store in production - memcached_value_max_megabytes = ENV.fetch("MEMCACHED_VALUE_MAX_MEGABYTES", 1).to_i - memcached_value_max_bytes = memcached_value_max_megabytes * 1024 * 1024 - config.cache_store = :mem_cache_store, { value_max_bytes: memcached_value_max_bytes } + config.cache_store = :redis_cache_store, { + driver: :hiredis, + url: ENV.fetch("OFN_REDIS_URL", "redis://localhost:6379/0"), + namespace: "cache" + } # Enable serving of images, stylesheets, and JavaScripts from an asset server # config.action_controller.asset_host = "http://assets.example.com" From d6b3b0a3d37c6570c8877f57074df94038c33054 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Sat, 15 May 2021 11:36:19 +0100 Subject: [PATCH 2/2] Ensure a basic level of fault-tolerance on Redis connections The default for reconnect_attempts is 0, which is not helpful! 1 is generally considered to be enough. --- config/environments/production.rb | 3 ++- config/environments/staging.rb | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/config/environments/production.rb b/config/environments/production.rb index cf90833eb2..472f47c050 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -45,7 +45,8 @@ Openfoodnetwork::Application.configure do config.cache_store = :redis_cache_store, { driver: :hiredis, url: ENV.fetch("OFN_REDIS_URL", "redis://localhost:6379/0"), - namespace: "cache" + namespace: "cache", + reconnect_attempts: 1 } # Enable serving of images, stylesheets, and JavaScripts from an asset server diff --git a/config/environments/staging.rb b/config/environments/staging.rb index cf90833eb2..472f47c050 100644 --- a/config/environments/staging.rb +++ b/config/environments/staging.rb @@ -45,7 +45,8 @@ Openfoodnetwork::Application.configure do config.cache_store = :redis_cache_store, { driver: :hiredis, url: ENV.fetch("OFN_REDIS_URL", "redis://localhost:6379/0"), - namespace: "cache" + namespace: "cache", + reconnect_attempts: 1 } # Enable serving of images, stylesheets, and JavaScripts from an asset server