From 0a25c2ae9a02d4480ef9a28028f6c4661fdd323d Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Sun, 8 Sep 2024 21:51:16 -0400 Subject: [PATCH] Play with solid_cache (#34) Reviewed-on: https://git.atomaka.com/atomaka/budget/pulls/34 --- Gemfile | 1 + Gemfile.lock | 5 +++++ config/database.yml | 19 +++++++++++++++---- config/environments/development.rb | 2 +- config/environments/production.rb | 2 +- config/solid_cache.yml | 16 ++++++++++++++++ db/cache_schema.rb | 14 ++++++++++++++ 7 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 config/solid_cache.yml create mode 100644 db/cache_schema.rb diff --git a/Gemfile b/Gemfile index 84f9cd1..4960cb9 100644 --- a/Gemfile +++ b/Gemfile @@ -18,6 +18,7 @@ gem "jbuilder" # Use Redis adapter to run Action Cable in production # gem "redis", ">= 4.0.1" +gem "solid_cache" gem "solid_queue" # Use Kredis to get higher-level data types in Redis [https://github.com/rails/kredis] diff --git a/Gemfile.lock b/Gemfile.lock index 9c8a670..5ed9d74 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -263,6 +263,10 @@ GEM rexml (~> 3.2, >= 3.2.5) rubyzip (>= 1.2.2, < 3.0) websocket (~> 1.0) + solid_cache (1.0.5) + activejob (>= 7.2) + activerecord (>= 7.2) + railties (>= 7.2) solid_queue (0.8.2) activejob (>= 7.1) activerecord (>= 7.1) @@ -325,6 +329,7 @@ DEPENDENCIES rails! rubocop-rails-omakase selenium-webdriver + solid_cache solid_queue sqlite3 (>= 1.4) stimulus-rails diff --git a/config/database.yml b/config/database.yml index fcba57f..87d3780 100644 --- a/config/database.yml +++ b/config/database.yml @@ -10,8 +10,14 @@ default: &default timeout: 5000 development: - <<: *default - database: db/development.sqlite3 + primary: + <<: *default + database: db/development.sqlite3 + cache: + <<: *default + database: db/development_cache.sqlite3 + migrations_paths: db/cache_migrate + # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". @@ -21,5 +27,10 @@ test: database: db/test.sqlite3 production: - <<: *default - database: db/production.sqlite3 + primary: + <<: *default + database: storage/production.sqlite3 + cache: + <<: *default + database: storage/production_cache.sqlite3 + migrations_paths: db/cache_migrate diff --git a/config/environments/development.rb b/config/environments/development.rb index 1d7d909..8370994 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -28,7 +28,7 @@ Rails.application.configure do end # Change this to :null_store to avoid any caching - config.cache_store = :memory_store + config.cache_store = :solid_cache_store # Store uploaded files on the local file system (see config/storage.yml for options). config.active_storage.service = :local diff --git a/config/environments/production.rb b/config/environments/production.rb index 08d3893..452d6b8 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -66,7 +66,7 @@ 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 = :solid_cache_store # Use a real queuing backend for Active Job (and separate queues per environment). config.active_job.queue_adapter = :solid_queue diff --git a/config/solid_cache.yml b/config/solid_cache.yml new file mode 100644 index 0000000..e20236f --- /dev/null +++ b/config/solid_cache.yml @@ -0,0 +1,16 @@ +default: &default + database: cache + 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/db/cache_schema.rb b/db/cache_schema.rb new file mode 100644 index 0000000..1646384 --- /dev/null +++ b/db/cache_schema.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +ActiveRecord::Schema[7.1].define(version: 1) do + 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