Skip to content

Commit 28780de

Browse files
authored
Add Solid Cache (and get ready for Solid Queue) (rails#52790)
* Add Solid Cache (and get ready for Solid Queue)
1 parent 9a3ca1a commit 28780de

File tree

12 files changed

+110
-2
lines changed

12 files changed

+110
-2
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ gem "cssbundling-rails"
2121
gem "importmap-rails", ">= 1.2.3"
2222
gem "tailwindcss-rails"
2323
gem "dartsass-rails"
24+
gem "solid_cache"
2425
gem "kamal", require: false
2526
gem "thruster", require: false
2627
# require: false so bcrypt is loaded only when has_secure_password is used.

Gemfile.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,10 @@ GEM
545545
rake
546546
serverengine (~> 2.0.5)
547547
thor
548+
solid_cache (1.0.4)
549+
activejob (>= 7.2)
550+
activerecord (>= 7.2)
551+
railties (>= 7.2)
548552
sorted_set (1.0.3)
549553
rbtree
550554
set (~> 1.0)
@@ -691,6 +695,7 @@ DEPENDENCIES
691695
selenium-webdriver (>= 4.20.0)
692696
sidekiq
693697
sneakers
698+
solid_cache
694699
sprockets-rails (>= 2.0.0)
695700
sqlite3 (>= 2.0)
696701
stackprof

railties/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Use [Solid Cache](https://github.com/rails/solid_cache) as the default Rails.cache backend in production, configured as a separate cache database in config/database.yml.
2+
3+
*DHH*
4+
15
* Add Rails::Rack::SilenceRequest middleware and use it via `config.silence_healthcheck_path = path`
26
to silence requests to "/up". This prevents the Kamal-required healthchecks from clogging up
37
the production logs.

railties/lib/rails/generators/app_base.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ def self.add_shared_options_for(name)
114114
class_option :skip_kamal, type: :boolean, default: false,
115115
desc: "Skip Kamal setup"
116116

117+
class_option :skip_solid, type: :boolean, default: false,
118+
desc: "Skip Solid Cache & Queue setup"
119+
117120
class_option :dev, type: :boolean, default: nil,
118121
desc: "Set up the #{name} with Gemfile pointing to your Rails checkout"
119122

@@ -428,6 +431,10 @@ def skip_kamal?
428431
options[:skip_kamal]
429432
end
430433

434+
def skip_solid?
435+
options[:skip_active_record] || options[:skip_solid]
436+
end
437+
431438
class GemfileEntry < Struct.new(:name, :version, :comment, :options, :commented_out)
432439
def initialize(name, version, comment, options = {}, commented_out = false)
433440
super
@@ -746,6 +753,12 @@ def run_kamal
746753
template "config/deploy.yml", force: true
747754
end
748755

756+
def run_solid
757+
return if skip_solid? || !bundle_install?
758+
759+
rails_command "solid_cache:install"
760+
end
761+
749762
def add_bundler_platforms
750763
if bundle_install?
751764
# The vast majority of Rails apps will be deployed on `x86_64-linux`.

railties/lib/rails/generators/rails/app/app_generator.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,7 @@ def finish_template
575575
public_task :run_hotwire
576576
public_task :run_css
577577
public_task :run_kamal
578+
public_task :run_solid
578579

579580
def run_after_bundle_callbacks
580581
@after_bundle_callbacks.each(&:call)

railties/lib/rails/generators/rails/app/templates/Gemfile.tt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ source "https://rubygems.org"
1414

1515
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
1616
gem "tzinfo-data", platforms: %i[ <%= bundler_windows_platforms %> jruby ]
17+
<% unless options.skip_solid? -%>
18+
19+
# Use the database-backed Solid Cache adapter for Rails.cache [https://github.com/rails/solid_cache]
20+
gem "solid_cache"
21+
<% end -%>
1722
<% if depend_on_bootsnap? -%>
1823

1924
# Reduces boot times through caching; required in config/boot.rb

railties/lib/rails/generators/rails/app/templates/config/databases/mysql.yml.tt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,21 @@ test:
5252
# Read https://guides.rubyonrails.org/configuring.html#configuring-a-database
5353
# for a full overview on how database connection configuration can be specified.
5454
#
55+
<%- if options.skip_solid? -%>
5556
production:
5657
<<: *default
5758
database: <%= app_name %>_production
5859
username: <%= app_name %>
5960
password: <%%= ENV["<%= app_name.upcase %>_DATABASE_PASSWORD"] %>
61+
<%- else -%>
62+
production:
63+
primary: &primary_production
64+
<<: *default
65+
database: <%= app_name %>_production
66+
username: <%= app_name %>
67+
password: <%%= ENV["<%= app_name.upcase %>_DATABASE_PASSWORD"] %>
68+
cache:
69+
<<: *primary_production
70+
database: <%= app_name %>_production_cache
71+
migrations_paths: db/cache_migrate
72+
<%- end -%>

railties/lib/rails/generators/rails/app/templates/config/databases/postgresql.yml.tt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,21 @@ test:
8484
# Read https://guides.rubyonrails.org/configuring.html#configuring-a-database
8585
# for a full overview on how database connection configuration can be specified.
8686
#
87+
<%- if options.skip_solid? -%>
8788
production:
8889
<<: *default
8990
database: <%= app_name %>_production
9091
username: <%= app_name %>
9192
password: <%%= ENV["<%= app_name.upcase %>_DATABASE_PASSWORD"] %>
93+
<%- else -%>
94+
production:
95+
primary: &primary_production
96+
<<: *default
97+
database: <%= app_name %>_production
98+
username: <%= app_name %>
99+
password: <%%= ENV["<%= app_name.upcase %>_DATABASE_PASSWORD"] %>
100+
cache:
101+
<<: *primary_production
102+
database: <%= app_name %>_production_cache
103+
migrations_paths: db/cache_migrate
104+
<%- end -%>

railties/lib/rails/generators/rails/app/templates/config/databases/sqlite3.yml.tt

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,35 @@ test:
2828
#
2929
# Similarly, if you deploy your application as a Docker container, you must
3030
# ensure the database is located in a persisted volume.
31+
<%- if options.skip_solid? -%>
3132
production:
3233
<<: *default
3334
# database: path/to/persistent/storage/production.sqlite3
3435
<%- else -%>
36+
production:
37+
primary:
38+
<<: *default
39+
# database: path/to/persistent/storage/production.sqlite3
40+
cache:
41+
<<: *default
42+
# database: path/to/persistent/storage/production_cache.sqlite3
43+
migrations_paths: db/cache_migrate
44+
<%- end -%>
45+
<%- else -%>
3546
# Store production database in the storage/ directory, which by default
3647
# is mounted as a persistent Docker volume in config/deploy.yml.
48+
<%- if options.skip_solid? -%>
3749
production:
3850
<<: *default
3951
database: storage/production.sqlite3
40-
<%- end -%>
52+
<%- else -%>
53+
production:
54+
primary:
55+
<<: *default
56+
database: storage/production.sqlite3
57+
cache:
58+
<<: *default
59+
database: storage/production_cache.sqlite3
60+
migrations_paths: db/cache_migrate
61+
<%- end -%>
62+
<%- end -%>

railties/lib/rails/generators/rails/app/templates/config/databases/trilogy.yml.tt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,21 @@ test:
5252
# Read https://guides.rubyonrails.org/configuring.html#configuring-a-database
5353
# for a full overview on how database connection configuration can be specified.
5454
#
55+
<%- if options.skip_solid? -%>
5556
production:
5657
<<: *default
5758
database: <%= app_name %>_production
5859
username: <%= app_name %>
5960
password: <%%= ENV["<%= app_name.upcase %>_DATABASE_PASSWORD"] %>
61+
<%- else -%>
62+
production:
63+
primary: &primary_production
64+
<<: *default
65+
database: <%= app_name %>_production
66+
username: <%= app_name %>
67+
password: <%%= ENV["<%= app_name.upcase %>_DATABASE_PASSWORD"] %>
68+
cache:
69+
<<: *primary_production
70+
database: <%= app_name %>_production_cache
71+
migrations_paths: db/cache_migrate
72+
<%- end -%>

0 commit comments

Comments
 (0)