From b2cfa725f57e0febeb507634a67d212f804c33df Mon Sep 17 00:00:00 2001 From: aidewoode Date: Fri, 2 Feb 2024 13:11:26 +0800 Subject: [PATCH] Replace litecache with solid_cache --- Gemfile | 3 +++ Gemfile.lock | 5 +++++ config/environments/development.rb | 2 +- config/environments/production.rb | 2 +- config/environments/test.rb | 2 +- ...024630_create_solid_cache_entries.solid_cache.rb | 12 ++++++++++++ ..._byte_size_to_solid_cache_entries.solid_cache.rb | 9 +++++++++ ...onstraints_to_solid_cache_entries.solid_cache.rb | 12 ++++++++++++ ...ey_index_from_solid_cache_entries.solid_cache.rb | 8 ++++++++ db/schema.rb | 13 ++++++++++++- 10 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20240202024630_create_solid_cache_entries.solid_cache.rb create mode 100644 db/migrate/20240202024631_add_key_hash_and_byte_size_to_solid_cache_entries.solid_cache.rb create mode 100644 db/migrate/20240202024632_add_key_hash_and_byte_size_indexes_and_null_constraints_to_solid_cache_entries.solid_cache.rb create mode 100644 db/migrate/20240202024633_remove_key_index_from_solid_cache_entries.solid_cache.rb diff --git a/Gemfile b/Gemfile index e7ff9677..3e346695 100644 --- a/Gemfile +++ b/Gemfile @@ -25,6 +25,9 @@ gem "puma", "~> 6.4.0" # Default database gem "sqlite3", "~> 1.7.0" +# Cache store +gem "solid_cache", "~> 0.4.2" + # Background job processing gem "solid_queue", "~> 0.2.1" diff --git a/Gemfile.lock b/Gemfile.lock index ea2064a7..63d69506 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -335,6 +335,10 @@ GEM simplecov-lcov (0.8.0) simplecov_json_formatter (0.1.4) smart_properties (1.17.0) + solid_cache (0.4.2) + activejob (>= 7) + activerecord (>= 7) + railties (>= 7) solid_queue (0.2.1) rails (~> 7.1) sqlite3 (1.7.0) @@ -416,6 +420,7 @@ DEPENDENCIES sidekiq (~> 7.1.2) simplecov (~> 0.22.0) simplecov-lcov (~> 0.8.0) + solid_cache (~> 0.4.2) solid_queue (~> 0.2.1) sqlite3 (~> 1.7.0) standard (~> 1.25.0) diff --git a/config/environments/development.rb b/config/environments/development.rb index d9cf4724..5e0d8801 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -23,7 +23,7 @@ config.action_controller.perform_caching = true config.action_controller.enable_fragment_cache_logging = true - config.cache_store = :memory_store + config.cache_store = :solid_cache_store config.public_file_server.headers = { "Cache-Control" => "public, max-age=#{2.days.to_i}" } diff --git a/config/environments/production.rb b/config/environments/production.rb index 0fc7835e..a098e217 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -65,7 +65,7 @@ config.log_level = ENV.fetch("RAILS_LOG_LEVEL", "info") # Use a different cache store in production. - config.cache_store = BlackCandy::Config.redis_cache_url.present? ? [:redis_cache_store, {url: BlackCandy::Config.redis_cache_url}] : [:litecache, {path: "storage/production_cache.sqlite3"}] + config.cache_store = BlackCandy::Config.redis_cache_url.present? ? [:redis_cache_store, {url: BlackCandy::Config.redis_cache_url}] : :solid_cache_store # Use a real queuing backend for Active Job (and separate queues per environment). config.active_job.queue_adapter = BlackCandy::Config.redis_sidekiq_url.present? ? :sidekiq : :solid_queue diff --git a/config/environments/test.rb b/config/environments/test.rb index cd4e0a60..514f0bf7 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -26,7 +26,7 @@ # Show full error reports and disable caching. config.consider_all_requests_local = true config.action_controller.perform_caching = false - config.cache_store = :memory_store + config.cache_store = :solid_cache_store # Raise exceptions instead of rendering exception templates. config.action_dispatch.show_exceptions = :rescuable diff --git a/db/migrate/20240202024630_create_solid_cache_entries.solid_cache.rb b/db/migrate/20240202024630_create_solid_cache_entries.solid_cache.rb new file mode 100644 index 00000000..04af3478 --- /dev/null +++ b/db/migrate/20240202024630_create_solid_cache_entries.solid_cache.rb @@ -0,0 +1,12 @@ +# This migration comes from solid_cache (originally 20230724121448) +class CreateSolidCacheEntries < ActiveRecord::Migration[7.0] + def change + create_table :solid_cache_entries do |t| + t.binary :key, null: false, limit: 1024 + t.binary :value, null: false, limit: 512.megabytes + t.datetime :created_at, null: false + + t.index :key, unique: true + end + end +end diff --git a/db/migrate/20240202024631_add_key_hash_and_byte_size_to_solid_cache_entries.solid_cache.rb b/db/migrate/20240202024631_add_key_hash_and_byte_size_to_solid_cache_entries.solid_cache.rb new file mode 100644 index 00000000..25d086ed --- /dev/null +++ b/db/migrate/20240202024631_add_key_hash_and_byte_size_to_solid_cache_entries.solid_cache.rb @@ -0,0 +1,9 @@ +# This migration comes from solid_cache (originally 20240108155507) +class AddKeyHashAndByteSizeToSolidCacheEntries < ActiveRecord::Migration[7.1] + def change + change_table :solid_cache_entries do |t| + t.column :key_hash, :integer, null: true, limit: 8 + t.column :byte_size, :integer, null: true, limit: 4 + end + end +end diff --git a/db/migrate/20240202024632_add_key_hash_and_byte_size_indexes_and_null_constraints_to_solid_cache_entries.solid_cache.rb b/db/migrate/20240202024632_add_key_hash_and_byte_size_indexes_and_null_constraints_to_solid_cache_entries.solid_cache.rb new file mode 100644 index 00000000..5c1561d5 --- /dev/null +++ b/db/migrate/20240202024632_add_key_hash_and_byte_size_indexes_and_null_constraints_to_solid_cache_entries.solid_cache.rb @@ -0,0 +1,12 @@ +# This migration comes from solid_cache (originally 20240110111600) +class AddKeyHashAndByteSizeIndexesAndNullConstraintsToSolidCacheEntries < ActiveRecord::Migration[7.1] + def change + change_table :solid_cache_entries, bulk: true do |t| + t.change_null :key_hash, false + t.change_null :byte_size, false + t.index :key_hash, unique: true + t.index [:key_hash, :byte_size] + t.index :byte_size + end + end +end diff --git a/db/migrate/20240202024633_remove_key_index_from_solid_cache_entries.solid_cache.rb b/db/migrate/20240202024633_remove_key_index_from_solid_cache_entries.solid_cache.rb new file mode 100644 index 00000000..b3145c06 --- /dev/null +++ b/db/migrate/20240202024633_remove_key_index_from_solid_cache_entries.solid_cache.rb @@ -0,0 +1,8 @@ +# This migration comes from solid_cache (originally 20240110111702) +class RemoveKeyIndexFromSolidCacheEntries < ActiveRecord::Migration[7.1] + def change + change_table :solid_cache_entries do |t| + t.remove_index :key, unique: true + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 5aa342e6..f9a74205 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_01_22_024820) do +ActiveRecord::Schema[7.1].define(version: 2024_02_02_024633) do create_table "active_storage_attachments", force: :cascade do |t| t.string "name", null: false t.string "record_type", null: false @@ -91,6 +91,17 @@ t.index ["singleton_guard"], name: "index_settings_on_singleton_guard", unique: true end + 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 + create_table "solid_queue_blocked_executions", force: :cascade do |t| t.integer "job_id", null: false t.string "queue_name", null: false