Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@zzak
Copy link
Member

@zzak zzak commented Oct 29, 2024

We have documented the ability to disable the query cache from db config, as well as implicitly being able to modify the size of the cache:

@query_cache_max_size = \
case query_cache = db_config&.query_cache
when 0, false
nil
when Integer
query_cache
when nil
DEFAULT_SIZE
end

This was added in #48110.

When updating an app to Rails 7.1, we wanted to keep using the cache unbounded while we put in place the proper instrumentation for observing that change.

Here is a full reproducible script:
# config/database.yml
development:
  adapter: sqlite3
  database: ":memory:"
  pool: 5
  timeout: 5000
  query_cache: false
# bug.rb

# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  gem "rails"
  gem "sqlite3"
end

require "active_record"
require "active_record/railtie"
require "logger"

class TestApp < Rails::Application
  config.load_defaults Rails::VERSION::STRING.to_f

  config.root = __dir__
  config.hosts << "example.org"
  config.eager_load = false
  config.session_store :cookie_store, key: "cookie_store_key"
  config.secret_key_base = "secret_key_base"

  config.logger = Logger.new($stdout)
  Rails.logger  = config.logger
end

puts "Rails version: #{Rails.version}"

ActiveRecord::Base.pluralize_table_names = false
ActiveRecord::Base.logger = Logger.new(STDOUT)

Rails.application.initialize!

ActiveRecord::Schema.define do
  create_table :one
  create_table :two
  create_table :three
  create_table :four
  create_table :five
end

class One < ActiveRecord::Base; end
class Two < ActiveRecord::Base; end
class Three < ActiveRecord::Base; end
class Four < ActiveRecord::Base; end
class Five < ActiveRecord::Base; end

require "minitest/autorun"
require "rails/test_help"

class BugTest < ActiveSupport::TestCase
  def setup
    @mapping = {
      1 => "One",
      2 => "Two",
      3 => "Three",
      4 => "Four",
      5 => "Five",
    }
  end

  def test_association_stuff
    (1..5).each do |i|
      @mapping[i].constantize.create!
    end

    (1..5).each do |i|
      assert_queries_count(1) do
        @mapping[i].constantize.first
      end
    end

    assert_queries_count(1) do
      One.first
    end

    assert_equal 0, ActiveRecord::Base.connection.query_cache.size
  end
end

/cc @byroot

@zzak zzak force-pushed the query_cache-config-disable branch from 0d2fab2 to fc04c70 Compare October 29, 2024 14:10
@zzak zzak force-pushed the query_cache-config-disable branch from fc04c70 to 4885e9a Compare October 29, 2024 14:15
@byroot byroot merged commit 723d410 into rails:main Oct 29, 2024
byroot added a commit that referenced this pull request Oct 29, 2024
Respect db config `query_cache` in railtie executor hook
byroot added a commit that referenced this pull request Oct 29, 2024
Respect db config `query_cache` in railtie executor hook
byroot added a commit that referenced this pull request Oct 30, 2024
Respect db config `query_cache` in railtie executor hook
jhawthorn added a commit that referenced this pull request Oct 31, 2024
@jhawthorn
Copy link
Member

@byroot @zzak Sorry! I had to revert this from the 7-1-stable branch where it was failing CI in order to cut a release. We should probably not backport this that far as 7.1 is now outside of bugfix support.

@zzak zzak deleted the query_cache-config-disable branch October 31, 2024 01:31
@zzak
Copy link
Member Author

zzak commented Oct 31, 2024

My bad, I should have checked the build sooner, thanks for take care of it 🙇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants