Skip to content

Commit

Permalink
Default to cache.yml instead of solid_cache.yml
Browse files Browse the repository at this point in the history
We'll fall back to the old filename if cache.yml doesn't exist.
  • Loading branch information
djmb committed Sep 10, 2024
1 parent a4da863 commit 4a7e97d
Show file tree
Hide file tree
Showing 19 changed files with 31 additions and 32 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Solid Cache is configured by default in new Rails 8 applications. But if you're
1. `bundle add solid_cache`
2. `bin/rails solid_cache:install`

This will configure Solid Cache as the production cache store, create `config/solid_cache.yml`, and create `db/cache_schema.rb`.
This will configure Solid Cache as the production cache store, create `config/cache.yml`, and create `db/cache_schema.rb`.

You will then have to add the configuration for the queue database in `config/database.yml`. If you're using sqlite, it'll look like this:

Expand Down Expand Up @@ -43,7 +43,7 @@ Then run `db:prepare` in production to ensure the database is created and the sc

## Configuration

Configuration will be read from `config/solid_cache.yml`. You can change the location of the config file by setting the `SOLID_CACHE_CONFIG` env variable.
Configuration will be read from `config/cache.yml` or `config/solid_cache.yml`. You can change the location of the config file by setting the `SOLID_CACHE_CONFIG` env variable.

The format of the file is:

Expand Down Expand Up @@ -169,7 +169,7 @@ production:
```

```yaml
# config/solid_cache.yml
# config/cache.yml
production:
databases: [cache_shard1, cache_shard2, cache_shard3]
```
Expand All @@ -179,7 +179,7 @@ production:
To encrypt the cache values, you can add the encrypt property.

```yaml
# config/solid_cache.yml
# config/cache.yml
production:
encrypt: true
```
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ configs.each do |config|
if config == :default
sh("bin/rails test")
else
sh("SOLID_CACHE_CONFIG=config/solid_cache_#{config}.yml bin/rails test")
sh("SOLID_CACHE_CONFIG=config/cache_#{config}.yml bin/rails test")
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/solid_cache/install/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class SolidCache::InstallGenerator < Rails::Generators::Base
source_root File.expand_path("templates", __dir__)

def copy_files
template "config/solid_cache.yml"
template "config/cache.yml"
template "db/cache_schema.rb"
end

Expand Down
11 changes: 7 additions & 4 deletions lib/solid_cache/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ class Engine < ::Rails::Engine
config.solid_cache = ActiveSupport::OrderedOptions.new

initializer "solid_cache.config", before: :initialize_cache do |app|
app.paths.add "config/solid_cache", with: ENV["SOLID_CACHE_CONFIG"] || "config/solid_cache.yml"
config_paths = %w[config/cache config/solid_cache]

options = {}
if (config_path = Pathname.new(app.config.paths["config/solid_cache"].first)).exist?
options = app.config_for(config_path).to_h.deep_symbolize_keys
config_paths.each do |path|
app.paths.add path, with: ENV["SOLID_CACHE_CONFIG"] || "#{path}.yml"
end

config_pathname = config_paths.map { |path| Pathname.new(app.config.paths[path].first) }.find(&:exist?)

options = config_pathname ? app.config_for(config_pathname).to_h.deep_symbolize_keys : {}

options[:connects_to] = config.solid_cache.connects_to if config.solid_cache.connects_to
options[:size_estimate_samples] = config.solid_cache.size_estimate_samples if config.solid_cache.size_estimate_samples
options[:encrypt] = config.solid_cache.encrypt if config.solid_cache.encrypt
Expand Down
6 changes: 1 addition & 5 deletions test/dummy/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,11 @@ class Application < Rails::Application

config.active_record.encryption.key_provider = ActiveRecord::Encryption::EnvelopeEncryptionKeyProvider.new

if ENV["SOLID_CACHE_CONFIG"] == "config/solid_cache_encrypted_custom.yml"
if ENV["SOLID_CACHE_CONFIG"] == "config/cache_encrypted_custom.yml"
config.solid_cache.encryption_context_properties = {
encryptor: ActiveRecord::Encryption::Encryptor.new,
message_serializer: ActiveRecord::Encryption::MessageSerializer.new
}
end

initializer :custom_solid_cache_yml, before: :solid_cache do |app|
app.paths.add "config/solid_cache", with: ENV["SOLID_CACHE_CONFIG_PATH"]
end
end
end
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class SolidCache::InstallGeneratorTest < Rails::Generators::TestCase

test "generator updates environment config" do
run_generator
assert_file "#{destination_root}/config/solid_cache.yml", expected_solid_cache_config
assert_file "#{destination_root}/config/cache.yml", expected_cache_config
assert_file "#{destination_root}/db/cache_schema.rb"
assert_file "#{destination_root}/config/environments/development.rb", /config.cache_store = :memory_store\n/
assert_file "#{destination_root}/config/environments/development.rb", /config.cache_store = :null_store\n/
Expand All @@ -27,7 +27,7 @@ class SolidCache::InstallGeneratorTest < Rails::Generators::TestCase
end

private
def expected_solid_cache_config
def expected_cache_config
<<~YAML
default: &default
store_options:
Expand Down
18 changes: 9 additions & 9 deletions test/models/solid_cache/record_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@

module SolidCache
class RecordTest < ActiveSupport::TestCase
SINGLE_DB_CONFIGS = [ "config/solid_cache_database.yml", "config/solid_cache_unprepared_statements.yml" ]
SINGLE_DB_CONFIGS = [ "config/cache_database.yml", "config/cache_unprepared_statements.yml" ]
MULTI_DB_CONFIGS = [
"config/solid_cache_connects_to.yml",
"config/solid_cache_encrypted.yml",
"config/solid_cache_encrypted_custom.yml",
"config/solid_cache_shards.yml",
"config/cache_connects_to.yml",
"config/cache_encrypted.yml",
"config/cache_encrypted_custom.yml",
"config/cache_shards.yml",
nil
]

test "each_shard" do
shards = SolidCache::Record.each_shard.map { SolidCache::Record.current_shard }
case ENV["SOLID_CACHE_CONFIG"]
when "config/solid_cache_no_database.yml"
when "config/cache_no_database.yml"
assert_equal [ :default ], shards
when "config/solid_cache_database.yml"
when "config/cache_database.yml"
assert_equal [ :primary ], shards
when "config/solid_cache_unprepared_statements.yml"
when "config/cache_unprepared_statements.yml"
assert_equal [ :primary_unprepared_statements ], shards
when *MULTI_DB_CONFIGS
assert_equal [ :primary_shard_one, :primary_shard_two, :secondary_shard_one, :secondary_shard_two ], shards
Expand All @@ -32,7 +32,7 @@ class RecordTest < ActiveSupport::TestCase
test "each_shard uses the default role" do
role = ActiveRecord::Base.connected_to(role: :reading) { SolidCache::Record.each_shard.map { SolidCache::Record.current_role } }
case ENV["SOLID_CACHE_CONFIG"]
when "config/solid_cache_no_database.yml"
when "config/cache_no_database.yml"
assert_equal [ :reading ], role
when *SINGLE_DB_CONFIGS
assert_equal [ :writing ], role
Expand Down
4 changes: 2 additions & 2 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ def second_shard_key
end

def single_database?
[ "config/solid_cache_database.yml", "config/solid_cache_no_database.yml", "config/solid_cache_unprepared_statements.yml" ].include?(ENV["SOLID_CACHE_CONFIG"])
[ "config/cache_database.yml", "config/cache_no_database.yml", "config/cache_unprepared_statements.yml" ].include?(ENV["SOLID_CACHE_CONFIG"])
end

def default_database?
ENV["SOLID_CACHE_CONFIG"] == "config/solid_cache_no_database.yml"
ENV["SOLID_CACHE_CONFIG"] == "config/cache_no_database.yml"
end

def shard_keys(cache, shard)
Expand Down
6 changes: 3 additions & 3 deletions test/unit/encryption_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ class SolidCache::EncryptionTest < ActiveSupport::TestCase
end

test "not encrypted" do
skip if ENV["SOLID_CACHE_CONFIG"] =~ /config\/solid_cache_encrypted.*\.yml/
skip if ENV["SOLID_CACHE_CONFIG"] =~ /config\/cache_encrypted.*\.yml/

@cache.write(@shard_key, "value")
assert_not_nil first_value
assert_equal raw_first_value, first_value
end

test "encrypted with defaults" do
skip unless ENV["SOLID_CACHE_CONFIG"] == "config/solid_cache_encrypted.yml"
skip unless ENV["SOLID_CACHE_CONFIG"] == "config/cache_encrypted.yml"

@cache.write(@shard_key, "value")
assert_not_nil first_value
Expand All @@ -28,7 +28,7 @@ class SolidCache::EncryptionTest < ActiveSupport::TestCase
end

test "encrypted with custom settings" do
skip unless ENV["SOLID_CACHE_CONFIG"] == "config/solid_cache_encrypted_custom.yml"
skip unless ENV["SOLID_CACHE_CONFIG"] == "config/cache_encrypted_custom.yml"

@cache.write(@shard_key, "value")
assert_not_nil first_value
Expand Down
2 changes: 1 addition & 1 deletion test/unit/stats_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class SolidCache::StatsTest < ActiveSupport::TestCase
end

def test_stats_with_entries_no_shards
skip unless ENV["SOLID_CACHE_CONFIG"] == "config/solid_cache_no_database.yml"
skip unless ENV["SOLID_CACHE_CONFIG"] == "config/cache_no_database.yml"

@cache = lookup_store(expiry_batch_size: 2, max_age: 2.weeks.to_i, max_entries: 1000)

Expand Down

0 comments on commit 4a7e97d

Please sign in to comment.