From b5e2b4c9d33dad4647cecc1c2de11f6cff0d3ef8 Mon Sep 17 00:00:00 2001 From: Yutaka Kamei Date: Sun, 12 Nov 2023 22:46:34 +0900 Subject: [PATCH] Set explicit string key for Rails cache (#123) Thanks to https://github.com/rails/rails/pull/50008, I found that the Rails cache key should be treated as String. This patch changes the Rails cache to set String instead of Integer to follow the expected cache key type. --- test/dummy/app/controllers/users_controller.rb | 2 +- test/rails_band/active_support/event/cache_generate_test.rb | 2 +- test/rails_band/active_support/event/cache_read_test.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/dummy/app/controllers/users_controller.rb b/test/dummy/app/controllers/users_controller.rb index 2b9d84f..b821ca4 100644 --- a/test/dummy/app/controllers/users_controller.rb +++ b/test/dummy/app/controllers/users_controller.rb @@ -70,7 +70,7 @@ def welcome_email def cache user = User.find(params.require(:user_id)) - result = Rails.cache.fetch(user.id, expires_in: 1.minutes) do + result = Rails.cache.fetch("key_#{user.id}", expires_in: 1.minutes) do user.slow_method end logger.info(result) diff --git a/test/rails_band/active_support/event/cache_generate_test.rb b/test/rails_band/active_support/event/cache_generate_test.rb index 6226c09..bbd837d 100644 --- a/test/rails_band/active_support/event/cache_generate_test.rb +++ b/test/rails_band/active_support/event/cache_generate_test.rb @@ -82,7 +82,7 @@ class CacheGenerateTest < ActionDispatch::IntegrationTest test 'returns key' do get "/users/#{@user.id}/cache" - assert_equal @user.id, @event.key + assert_equal "key_#{@user.id}", @event.key end if Gem::Version.new(Rails.version) >= Gem::Version.new('6.1') diff --git a/test/rails_band/active_support/event/cache_read_test.rb b/test/rails_band/active_support/event/cache_read_test.rb index 50a5fa9..bf74eed 100644 --- a/test/rails_band/active_support/event/cache_read_test.rb +++ b/test/rails_band/active_support/event/cache_read_test.rb @@ -82,7 +82,7 @@ class CacheReadTest < ActionDispatch::IntegrationTest test 'returns key' do get "/users/#{@user.id}/cache" - assert_equal @user.id, @event.key + assert_equal "key_#{@user.id}", @event.key end if Gem::Version.new(Rails.version) >= Gem::Version.new('6.1')