From 1e2d5debaf041461362a5221be5ccce60cb0b843 Mon Sep 17 00:00:00 2001 From: Kota Shiratsuka Date: Wed, 27 Nov 2024 10:51:19 +0900 Subject: [PATCH 2/2] Calling MiniMagick every time is slow, so I'm going to put it in Redis. --- app/helpers/application_helper.rb | 34 ++++++++++++++++++++++--------- config/environments/production.rb | 1 + 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 91a43c9f3..593a2c1fb 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1007,18 +1007,32 @@ module ApplicationHelper title_and_alt = other_attrs.scan(title_and_alt_re).to_h other_attrs.gsub!(title_and_alt_re, '') - # Process dimensions and scaling - width, height = nil, nil - image = MiniMagick::Image.open(found.diskfile) - width, height = image.width, image.height - - # Adjust dimensions for @2x, @3x, etc. - if filename =~ /@(\d+)x\./ - scale_factor = $1.to_i - width /= scale_factor - height /= scale_factor + # Get cache store configuration + config = Rails.application.config.redmine_image_cache_store rescue :memory_store + cache_store = ActiveSupport::Cache.lookup_store(config) + + # Use digest from the database as cache key + cache_key = "image_dimensions:#{found.digest}" + + # Cache dimensions and retrieve from Redis or memory store + dimensions = cache_store.fetch(cache_key) do + # Process dimensions and scaling + image = MiniMagick::Image.open(found.diskfile) + width, height = image.width, image.height + + # Adjust dimensions for @2x, @3x, etc. + if filename =~ /@(\d+)x\./ + scale_factor = $1.to_i + width /= scale_factor + height /= scale_factor + end + + { width: width, height: height } end + width = dimensions[:width] + height = dimensions[:height] + # Add width and height to the img tag only if style attribute is absent dimension_attrs = (!has_style && width && height) ? " width=\"#{width}\" height=\"#{height}\"" : "" diff --git a/config/environments/production.rb b/config/environments/production.rb index cd0d67353..adca39d63 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -45,6 +45,7 @@ Rails.application.configure do # Using Redis Store config.cache_store = :redis_cache_store, { path: '/var/run/valkey/valkey.sock' } config.redmine_search_cache_store = :redis_cache_store, { path: '/var/run/valkey/valkey.sock' } + config.redmine_image_cache_store = :redis_cache_store, { path: '/var/run/valkey/valkey.sock' } # Send deprecation notices to registered listeners. config.active_support.deprecation = :log -- 2.47.1