From 283362e94fb3d6654df018008d7a3184561fa63d Mon Sep 17 00:00:00 2001 From: Mikk Romulus Date: Tue, 12 Nov 2024 15:10:37 +0200 Subject: [PATCH] chore: Set up solid cache --- Gemfile | 5 +---- Gemfile.lock | 12 +++++------- Makefile | 8 +++++--- README.md | 1 - config/cache.yml | 17 +++++++++++++++++ config/environments/development.rb | 2 +- config/environments/production.rb | 10 +--------- db/cache_schema.rb | 26 ++++++++++++++++++++++++++ docker/dev/docker-compose.yml | 17 ----------------- docker/prod/docker-compose.yml | 7 ------- 10 files changed, 56 insertions(+), 49 deletions(-) create mode 100644 config/cache.yml create mode 100644 db/cache_schema.rb diff --git a/Gemfile b/Gemfile index 3c99caed..85d89108 100644 --- a/Gemfile +++ b/Gemfile @@ -16,16 +16,13 @@ gem 'nilify_blanks', '~> 1.4' gem 'data_migrate' gem 'solid_cable' gem 'solid_queue' +gem 'solid_cache' # frontend gem 'haml-rails', '~> 2.0' gem 'turbo-rails', '~> 2.0' gem 'view_component' -# TO BE REMOVED -gem 'redis' -gem 'hiredis', '~> 0.6.3' - # functionality gem 'stringex', '~> 2.8', require: 'stringex_lite' gem 'http', '~> 5.0' diff --git a/Gemfile.lock b/Gemfile.lock index 50d75717..becb3f8f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -182,7 +182,6 @@ GEM haml (>= 4.0.6) railties (>= 5.1) hashie (5.0.0) - hiredis (0.6.3) http (5.2.0) addressable (~> 2.8) base64 (~> 0.1) @@ -380,10 +379,6 @@ GEM rdoc (6.7.0) psych (>= 4.0.0) redcarpet (3.6.0) - redis (5.3.0) - redis-client (>= 0.22.0) - redis-client (0.22.2) - connection_pool regexp_parser (2.9.2) reline (0.5.11) io-console (~> 0.5) @@ -472,6 +467,10 @@ GEM activejob (>= 7.2) activerecord (>= 7.2) railties (>= 7.2) + solid_cache (1.0.6) + activejob (>= 7.2) + activerecord (>= 7.2) + railties (>= 7.2) solid_queue (1.0.1) activejob (>= 7.1) activerecord (>= 7.1) @@ -563,7 +562,6 @@ DEPENDENCIES flamegraph friendly_id (~> 5.5.0) haml-rails (~> 2.0) - hiredis (~> 0.6.3) http (~> 5.0) ipaddress! kaminari @@ -586,7 +584,6 @@ DEPENDENCIES rails-patterns rails-pg-extras redcarpet - redis rgl rouge rspec-rails @@ -596,6 +593,7 @@ DEPENDENCIES silencer simple_form (~> 5.3) solid_cable + solid_cache solid_queue sqlite3 stackprof diff --git a/Makefile b/Makefile index 55d79f5b..67897302 100644 --- a/Makefile +++ b/Makefile @@ -36,8 +36,10 @@ console: .makerc-vars ## Open rails console logs: .makerc-vars ## Tail all logs $(SUDO_COMMAND) docker compose -f docker/$(DEPLOY_ENVIRONMENT)/docker-compose.yml logs -f --tail=100 -clear-redis: .makerc-vars ## Clear rails cache (by flushing redis) - $(SUDO_COMMAND) docker compose -f docker/$(DEPLOY_ENVIRONMENT)/docker-compose.yml exec redis redis-cli flushdb +clear-redis: clear-cache # dummy until migrated + +clear-cache: .makerc-vars ## Clear rails cache + $(SUDO_COMMAND) docker compose -f docker/$(DEPLOY_ENVIRONMENT)/docker-compose.yml exec web bin/rails r 'Rails.cache.clear' import-db: $(SUDO_COMMAND) docker compose -f docker/$(DEPLOY_ENVIRONMENT)/docker-compose.yml up -d postgresql --wait @@ -51,4 +53,4 @@ CURRENT_VERSION: .makerc-vars: @echo "\033[93m[*] Configuration file missing, running configurator\033[0m" @python3 scripts/generate_config.py - @if [ -f ".makerc-vars" ]; then echo "\033[92m[*] Config file created, please rerun the task!\033[0m"; false; else echo "\033[91m[*] Config file was not created, please rerun the task!\033[0m"; false; fi \ No newline at end of file + @if [ -f ".makerc-vars" ]; then echo "\033[92m[*] Config file created, please rerun the task!\033[0m"; false; else echo "\033[91m[*] Config file was not created, please rerun the task!\033[0m"; false; fi diff --git a/README.md b/README.md index 53f6f956..deccaae6 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,6 @@ Providentia is a [Ruby on Rails](https://github.com/rails/rails) based web appli Can be switched to any OpenID Connect provider -- ~~Redis~~[Garnet](https://github.com/microsoft/garnet) for caching and session storage - [PostgreSQL](https://www.postgresql.org/) - [Rails](https://github.com/rails/rails) app - [Caddy](https://github.com/caddyserver/caddy) reverse proxy diff --git a/config/cache.yml b/config/cache.yml new file mode 100644 index 00000000..25358872 --- /dev/null +++ b/config/cache.yml @@ -0,0 +1,17 @@ +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: + database: cache + <<: *default + +test: + <<: *default + +production: + database: cache + <<: *default diff --git a/config/environments/development.rb b/config/environments/development.rb index 0567f0c0..4b9b2deb 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -29,7 +29,7 @@ config.action_controller.perform_caching = false end - config.cache_store = :redis_cache_store, { url: ENV.fetch('REDIS_URL') { 'redis://localhost:6379/1' } } + 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 e3488312..77a2fe5a 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -49,15 +49,7 @@ config.active_support.report_deprecations = false # Replace the default in-process memory cache store with a durable alternative. - # config.cache_store = :mem_cache_store - config.cache_store = :redis_cache_store, { - url: ENV.fetch('REDIS_URL') { 'redis://localhost:6379/0' }, - - connect_timeout: 30, - read_timeout: 0.2, - write_timeout: 0.2, - reconnect_attempts: 1 - } + config.cache_store = :solid_cache_store # Replace the default in-process and non-durable queuing backend for Active Job. config.active_job.queue_adapter = :solid_queue diff --git a/db/cache_schema.rb b/db/cache_schema.rb new file mode 100644 index 00000000..fc4322ec --- /dev/null +++ b/db/cache_schema.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +# 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 + 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 diff --git a/docker/dev/docker-compose.yml b/docker/dev/docker-compose.yml index fea85a80..118c3eec 100644 --- a/docker/dev/docker-compose.yml +++ b/docker/dev/docker-compose.yml @@ -68,23 +68,9 @@ services: - KEYCLOAK_AVAILABILITYCHECK_TIMEOUT=30s - IMPORT_FILES='/config/*' - redis: - image: "redis:alpine" - volumes: - - "redis:/data" - - garnet: - image: "ghcr.io/microsoft/garnet" - ulimits: - memlock: -1 - volumes: - - garnetdata:/data - web: depends_on: - "postgresql" - - "redis" - - "garnet" build: context: ../../ dockerfile: Dockerfile @@ -112,7 +98,6 @@ services: VITE_RUBY_PORT: 80 VITE_RUBY_SKIP_PROXY: true DATABASE_URL: postgres://providentia:secret@postgresql/providentia?pool=5 - REDIS_URL: redis://garnet:6379/0 OIDC_ISSUER: http://keycloak.localhost/realms/Providentia OIDC_CLIENT_ID: Providentia OIDC_CLIENT_SECRET: 00000000-0000-0000-0000-000000000000 @@ -171,7 +156,5 @@ services: volumes: caddy_data: - redis: postgres_providentia: postgres_keycloak: - garnetdata: diff --git a/docker/prod/docker-compose.yml b/docker/prod/docker-compose.yml index 3d1afee5..e944baad 100644 --- a/docker/prod/docker-compose.yml +++ b/docker/prod/docker-compose.yml @@ -58,12 +58,6 @@ services: - POSTGRES_PASSWORD=secret - POSTGRES_DB=providentia - redis: - image: "redis:alpine" - restart: unless-stopped - volumes: - - "redis:/data" - keycloak: image: bitnami/keycloak:25.0.4 depends_on: @@ -98,6 +92,5 @@ services: - IMPORT_FILES='/config/*' volumes: - redis: {} caddy_data: {} providentia_db: {}