以下の内容はhttps://madogiwa0124.hatenablog.com/entry/2025/01/25/230015より取得しました。


PrimaryのDatabaseでSolidCacheを使うメモ📝

Herokuでホストしている個人サービスのcache storeをSolidCacheに移行したときに軽くハマったのでメモ📝

github.com

基本、既存のRailsアプリケーションをSolidCacheに乗り換えるときには普通にインストールしてbin/rails solid_cache:installを実行して設定ファイルを作成してconfig.cache_store = :solid_cache_storeに変更するぐらいで利用することができるのですが、SolidCacheのデフォルトはDatabaseがprimaryとは別のcacheを作成する構成になっており、Heroku Posgresでは複数のDatabaseを顕現的に作ることができず、この構成を取ろうとすると新しいHeroku Posgresqlを立てないといけずコスト的に厳しいです😢

Unlike Amazon RDS, Heroku doesn't allow creating multiple databases – your DB role simply doesn't have permissions to CREATE DATABASE ..;. https://stackoverflow.com/questions/45316858/is-it-possible-to-have-multiple-databases-per-one-heroku-postgres-plan

We typically see CONNECT privilege errors in cases where applications try to create databases as part of running db:setup, given that the Heroku Postgres credential provided does not have privileges to create or drop databases. https://help.heroku.com/63D7ALXT/why-am-i-seeing-user-does-not-have-connect-privilege-error-with-heroku-postgres-on-review-apps

そのためprimaryのデータベースに以下のように相乗りさせる形の構成にし、

config/cache.yml

development:
-  database: cache
+  database: primary
  <<: *default

test:
-  database: cache
+  database: primary
  <<: *default

production:
-  database: cache
+  database: primary
  encrypt: true
  <<: *default

SolidCacheではマイグレーションファイルを保持しておらずcache用DBのschema.rbをschema:loadで利用する感じになりますが、

github.com

それだとprimaryのdatabaseにマイグレーションが走らないので、以下のように上述のschema.rbの内容を参考にmigrationファイルを作成してprimaryのDBに対して実行することでprimaryのDBでSolidCache関連のtableを作成するようにしました。

db/migrate/20241227000000_create_solidcache_tables.rb

# NOTE: herokuではdatabeを複数持てないのでsolidcacheのschemaからコピペ
# https://github.com/rails/solid_cache/blob/v1.0.6/lib/generators/solid_cache/install/templates/db/cache_schema.rb
class CreateSolidcacheTables < ActiveRecord::Migration[7.2]
  def change
    create_table "solid_cache_entries", force: :cascade do |t|
      t.binary "key", limit: 1024, null: false
      t.binary "value", limit: 536870912, null: false
      t.datetime "created_at", null: false
      t.integer "key_hash", limit: 8, null: false
      t.integer "byte_size", limit: 4, null: false
      t.index ["byte_size"], name: "index_solid_cache_entries_on_byte_size"
      t.index ["key_hash", "byte_size"], name: "index_solid_cache_entries_on_key_hash_and_byte_size"
      t.index ["key_hash"], name: "index_solid_cache_entries_on_key_hash", unique: true
    end
  end
end

これでPrimaryのDatabaseでSolidCacheを使えるようになりました🎉




以上の内容はhttps://madogiwa0124.hatenablog.com/entry/2025/01/25/230015より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14