Introduce SolidCache

This commit is contained in:
Eugene Burmakin 2025-05-31 14:00:52 +02:00
parent a95d362b63
commit 3a955b8e51
8 changed files with 71 additions and 6 deletions

View file

@ -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'

View file

@ -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

15
config/cache.yml Normal file
View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

27
db/cache_schema.rb Normal file
View file

@ -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