From 3a955b8e51be8a34f7d8b0cf32bc40cbd8691fab Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Sat, 31 May 2025 14:00:52 +0200 Subject: [PATCH] Introduce SolidCache --- Gemfile | 1 + Gemfile.lock | 5 +++++ config/cache.yml | 15 ++++++++++++++ config/environments/development.rb | 6 +++--- config/environments/production.rb | 4 ++-- config/initializers/02_version_cache.rb | 8 +++++++- config/initializers/solid_cache.rb | 11 ++++++++++ db/cache_schema.rb | 27 +++++++++++++++++++++++++ 8 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 config/cache.yml create mode 100644 config/initializers/solid_cache.rb create mode 100644 db/cache_schema.rb diff --git a/Gemfile b/Gemfile index e5fd1134..ea404a18 100644 --- a/Gemfile +++ b/Gemfile @@ -44,6 +44,7 @@ gem 'sprockets-rails' gem 'stimulus-rails' gem 'strong_migrations' gem 'solid_cable', '~> 3.0' +gem 'solid_cache', '1.0.7' gem 'solid_queue', '~> 1.1' gem 'tailwindcss-rails' gem 'turbo-rails' diff --git a/Gemfile.lock b/Gemfile.lock index 35372631..091acecb 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -457,6 +457,10 @@ GEM activejob (>= 7.2) activerecord (>= 7.2) railties (>= 7.2) + solid_cache (1.0.7) + activejob (>= 7.2) + activerecord (>= 7.2) + railties (>= 7.2) solid_queue (1.1.5) activejob (>= 7.1) activerecord (>= 7.1) @@ -582,6 +586,7 @@ DEPENDENCIES sidekiq-limit_fetch simplecov solid_cable (~> 3.0) + solid_cache (= 1.0.7) solid_queue (~> 1.1) sprockets-rails stackprof diff --git a/config/cache.yml b/config/cache.yml new file mode 100644 index 00000000..040a2f5e --- /dev/null +++ b/config/cache.yml @@ -0,0 +1,15 @@ +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: + <<: *default diff --git a/config/environments/development.rb b/config/environments/development.rb index ff49d090..1ee6dff5 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -26,18 +26,18 @@ Rails.application.configure do # Enable/disable caching. By default caching is disabled. # Run rails dev:cache to toggle caching. + config.cache_store = :solid_cache_store + config.solid_cache.connects_to = { database: { writing: :cache } } + if Rails.root.join('tmp/caching-dev.txt').exist? config.action_controller.perform_caching = true config.action_controller.enable_fragment_cache_logging = true - config.cache_store = :redis_cache_store, { url: ENV['REDIS_URL'] } config.public_file_server.headers = { 'Cache-Control' => "public, max-age=#{2.days.to_i}" } else config.action_controller.perform_caching = false - - config.cache_store = :redis_cache_store, { url: ENV['REDIS_URL'] } end config.public_file_server.enabled = true diff --git a/config/environments/production.rb b/config/environments/production.rb index 8b4e7dd1..aac8634c 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -73,8 +73,8 @@ Rails.application.configure do config.log_level = ENV.fetch('RAILS_LOG_LEVEL', 'info') # Use a different cache store in production. - # config.cache_store = :mem_cache_store - config.cache_store = :redis_cache_store, { url: ENV['REDIS_URL'] } + config.cache_store = :solid_cache_store + config.solid_cache.connects_to = { database: { writing: :cache } } # Use a real queuing backend for Active Job (and separate queues per environment). config.active_job.queue_adapter = :solid_queue diff --git a/config/initializers/02_version_cache.rb b/config/initializers/02_version_cache.rb index c6fed3b3..bf59a9a3 100644 --- a/config/initializers/02_version_cache.rb +++ b/config/initializers/02_version_cache.rb @@ -1,3 +1,9 @@ # frozen_string_literal: true -Rails.cache.delete('dawarich/app-version-check') +# Defer cache operations until after initialization to avoid SolidCache loading issues +Rails.application.config.after_initialize do + # Skip cache clearing when running the Rails console + unless defined?(Rails::Console) || File.basename($PROGRAM_NAME) == 'rails' && ARGV.include?('console') + Rails.cache.delete('dawarich/app-version-check') if Rails.cache.respond_to?(:delete) + end +end diff --git a/config/initializers/solid_cache.rb b/config/initializers/solid_cache.rb new file mode 100644 index 00000000..15f78444 --- /dev/null +++ b/config/initializers/solid_cache.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +# Configure SolidCache +Rails.application.config.to_prepare do + # Only require the entries file as it seems the Entry class is defined there + begin + require 'solid_cache/store/entries' + rescue LoadError => e + Rails.logger.warn "Could not load SolidCache: #{e.message}" + end +end diff --git a/db/cache_schema.rb b/db/cache_schema.rb new file mode 100644 index 00000000..3cf97e44 --- /dev/null +++ b/db/cache_schema.rb @@ -0,0 +1,27 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# This file is the source Rails uses to define your schema when running `bin/rails +# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema[8.0].define(version: 1) do + # These are extensions that must be enabled in order to support this database + enable_extension "pg_catalog.plpgsql" + + create_table "solid_cache_entries", force: :cascade do |t| + t.binary "key", null: false + t.binary "value", null: false + t.datetime "created_at", null: false + t.bigint "key_hash", null: false + t.integer "byte_size", 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