Closed10
Rails8でSolid Cacheを使ってみる
デフォルト設定でインストールしたRails 8.0.0を使っていく
デフォで入っているのでinstallしても何も変わらず
$ bin/rails solid_cache:install
identical config/cache.yml
identical db/cache_schema.rb
gsub config/environments/production.rb
設定ファイルでもproductionのみSolid Cacheを使うようになっていた
config/cache.yml
default: &default
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
config/database.yml
# SQLite. Versions 3.8.0 and up are supported.
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem "sqlite3"
#
default: &default
adapter: sqlite3
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
development:
<<: *default
database: storage/development.sqlite3
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: storage/test.sqlite3
# Store production database in the storage/ directory, which by default
# is mounted as a persistent Docker volume in config/deploy.yml.
production:
primary:
<<: *default
database: storage/production.sqlite3
cache:
<<: *default
database: storage/production_cache.sqlite3
migrations_paths: db/cache_migrate
queue:
<<: *default
database: storage/production_queue.sqlite3
migrations_paths: db/queue_migrate
cable:
<<: *default
database: storage/production_cable.sqlite3
migrations_paths: db/cable_migrate
development環境にsolid cacheを設定する
設定ファイルの変更
config/cache.yml
default: &default
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:
+ database: cache
<<: *default
test:
<<: *default
production:
database: cache
<<: *default
config/database.yml
# SQLite. Versions 3.8.0 and up are supported.
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem "sqlite3"
#
default: &default
adapter: sqlite3
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
development:
- <<: *default
- database: storage/development.sqlite3
+ primary:
+ <<: *default
+ database: storage/development.sqlite3
+ cache:
+ <<: *default
+ database: storage/development_cache.sqlite3
+ migrations_paths: db/cache_migrate
Then run db:prepare in production to ensure the database is created and the schema is loaded.
公式の説明に従って rails db:prepare
実行
bin/rails db:prepare
ファイル(DB)ができた
prepareはこれ
rails console
から使ってみる
テーブルが無いと怒られた。
$ rails c
Loading development environment (Rails 8.0.0)
test-app(dev)> Rails.cache.read("test")
(test-app):1:in `<main>': Could not find table 'solid_cache_entries' (ActiveRecord::StatementInvalid)
rails db
で中身を確認すると存在するので接続先がcacheのDBになっていなさそう
$ rails db --db=cache
SQLite version 3.47.0 2024-10-21 16:30:22
Enter ".help" for usage hints.
sqlite> .table
ar_internal_metadata schema_migrations solid_cache_entries
どこかで config/cache.yml
の設定が巻き戻っていたぽいので戻したら正常に動作を確認
$ rails c
Loading development environment (Rails 8.0.0)
test-app(dev)> Rails.cache.read("test")
SolidCache::Entry Load (0.4ms) SELECT "solid_cache_entries"."key", "solid_cache_entries"."value" FROM "solid_cache_entries" WHERE "solid_cache_entries"."key_hash" IN (-8888549517541498432) /*application='TestApp'*/
=> nil
このスクラップは16日前にクローズされました