From 1f35c139e0c1859258ec54cf37884f8eebddcd5c Mon Sep 17 00:00:00 2001 From: Shinichi Maeshima Date: Tue, 29 Sep 2026 11:12:46 +0900 Subject: [PATCH] Use `shared` key instead of `default` in cache.yml The README uses `default` as the top-level key in the `config/cache.yml` sample. It looks like a place to define defaults available to every environment, but it is actually not a meaningful key name. Shared settings can be achieved with YAML anchors and aliases, but the current example is easy to misunderstand. Moreover, in the example, `size_estimate_samples: 1000` ends up being a setting that is never used. `config/cache.yml` is loaded with `config_for`: https://github.com/rails/solid_cache/blob/da70ef4f8f9c5ad6490b3994cd4d67dcf5e3659a/lib/solid_cache/engine.rb#L21 `config_for` treats `shared` as the defaults for every environment, so switch to it to reduce confusion. The template generated by `bin/rails solid_cache:install` is updated accordingly. --- README.md | 12 ++++++------ .../install/templates/config/cache.yml.tt | 9 +-------- .../solid_cache/install_generator_test.rb | 9 +-------- 3 files changed, 8 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index a6942ce..b0a187d 100644 --- a/README.md +++ b/README.md @@ -56,25 +56,25 @@ Configuration will be read from `config/cache.yml` or `config/solid_cache.yml`. The format of the file is: ```yml -default: - store_options: &default_store_options +shared: + store_options: max_age: <%= 60.days.to_i %> namespace: <%= Rails.env %> size_estimate_samples: 1000 -development: &development +development: database: cache store_options: - <<: *default_store_options max_size: <%= 256.gigabytes %> -production: &production +production: databases: [production_cache1, production_cache2] store_options: - <<: *default_store_options max_size: <%= 256.gigabytes %> ``` +The `shared` key is read by Rails' `config_for` and deep merged into the configuration of every environment, with the environment's own values taking precedence. + For the full list of keys for `store_options` see [Cache configuration](#cache-configuration). Any options passed to the cache lookup will overwrite those specified here. After running `solid_cache:install`, `environments/production.rb` will replace your cache store with Solid Cache, but you can also do this manually: diff --git a/lib/generators/solid_cache/install/templates/config/cache.yml.tt b/lib/generators/solid_cache/install/templates/config/cache.yml.tt index 70a2ac6..52b4a16 100644 --- a/lib/generators/solid_cache/install/templates/config/cache.yml.tt +++ b/lib/generators/solid_cache/install/templates/config/cache.yml.tt @@ -1,16 +1,9 @@ -default: &default +shared: store_options: # Cap age of oldest cache entry to fulfill retention policies # max_age: <%%= 60.days.to_i %> max_size: <%%= 256.megabytes %> namespace: <%%= Rails.env %> -development: - <<: *default - -test: - <<: *default - production: database: <%= ENV.fetch("DATABASE", "cache") %> - <<: *default diff --git a/test/lib/generators/solid_cache/solid_cache/install_generator_test.rb b/test/lib/generators/solid_cache/solid_cache/install_generator_test.rb index fd12cbc..83ce8f8 100644 --- a/test/lib/generators/solid_cache/solid_cache/install_generator_test.rb +++ b/test/lib/generators/solid_cache/solid_cache/install_generator_test.rb @@ -71,22 +71,15 @@ class SolidCache::InstallGeneratorTest < Rails::Generators::TestCase private def expected_cache_config <<~YAML - default: &default + shared: store_options: # Cap age of oldest cache entry to fulfill retention policies # max_age: <%= 60.days.to_i %> max_size: <%= 256.megabytes %> namespace: <%= Rails.env %> - development: - <<: *default - - test: - <<: *default - production: database: cache - <<: *default YAML end end