Skip to content

Self-hosted project management for devs and their clients: tickets, time tracking, docs and meetings, with an AI assistant that cites its sources. AGPL-3.0.

Sweep expired cache entries hourly and give the file store its own directory

Guido Boyke9232b59df21b
diff --git a/app/jobs/cache_cleanup_job.rb b/app/jobs/cache_cleanup_job.rb
new file mode 100644
index 0000000..52583f9
--- /dev/null
+++ b/app/jobs/cache_cleanup_job.rb
@@ -0,0 +1,23 @@
+# frozen_string_literal: true
+
+# Deletes expired cache entries, hourly (config/recurring.yml).
+#
+# Production caches in a file store (production.rb, tmp/cache/store), and a
+# file store never deletes an expired entry on its own: it only skips it on
+# read. Rack::Attack writes a counter per client and minute there, so without
+# this sweep the directory grows by every counter ever written, and whatever a
+# key named stays on the disk. Emptying tmp/ instead is no substitute: it also
+# deletes the server's socket under tmp/sockets. Entries without an expiry
+# stay, as in any store.
+#
+# Stores that expire entries themselves (Redis on staging, Memcache, Solid
+# Cache) raise NotImplementedError from #cleanup; there is nothing to do.
+class CacheCleanupJob < ApplicationJob
+ queue_as :default
+
+ def perform
+ Rails.cache.cleanup
+ rescue NotImplementedError
+ nil
+ end
+end
diff --git a/config/environments/production.rb b/config/environments/production.rb
index 7e0de93..d804bd2 100644
--- a/config/environments/production.rb
+++ b/config/environments/production.rb
@@ -119,8 +119,10 @@ Rails.application.configure do
end
end
- # Use a different cache store in production.
- # config.cache_store = :mem_cache_store
+ # A file store, as Rails' default, but in a directory of its own: the default
+ # is tmp/cache itself, which bootsnap and the asset caches share, and
+ # CacheCleanupJob (FileStore#cleanup) reads every file below the store.
+ config.cache_store = :file_store, Rails.root.join('tmp/cache/store')
# Use a real queuing backend for Active Job. Solid Queue runs on the primary
# database: the queue connection is pinned to :primary in
diff --git a/config/recurring.yml b/config/recurring.yml
index 5658ea7..d217af4 100644
--- a/config/recurring.yml
+++ b/config/recurring.yml
@@ -15,6 +15,13 @@ production:
clear_solid_queue_finished_jobs:
command: "SolidQueue::Job.clear_finished_in_batches(sleep_between_batches: 0.3)"
schedule: every hour at minute 12
+ # The file store deletes no expired entry on its own (CacheCleanupJob says
+ # why that matters). Hourly: a rate limit counter lives a minute, and nothing
+ # should outlive its expiry by much more than an hour.
+ cache_cleanup:
+ class: CacheCleanupJob
+ queue: default
+ schedule: every hour at minute 25
# GDPR: runs scheduled account deletions (scheduled_deletion_at exceeded)
# automatically — previously NEVER ran automatically (only manually via rails runner).
scheduled_account_deletions:
@@ -191,6 +198,11 @@ staging:
clear_solid_queue_finished_jobs:
command: "SolidQueue::Job.clear_finished_in_batches(sleep_between_batches: 0.3)"
schedule: every hour at minute 12
+ # A no-op on Redis, here so the schedule is the same as on production.
+ cache_cleanup:
+ class: CacheCleanupJob
+ queue: default
+ schedule: every hour at minute 25
scheduled_account_deletions:
class: ScheduledDeletionJob
queue: default
diff --git a/spec/config/instance_settings_spec.rb b/spec/config/instance_settings_spec.rb
index 78901cb..8e82a59 100644
--- a/spec/config/instance_settings_spec.rb
+++ b/spec/config/instance_settings_spec.rb
@@ -252,6 +252,18 @@ RSpec.describe 'config/environments/production.rb' do
end
end
+ # The cache gets a directory of its own under tmp/cache. Rails' default is
+ # tmp/cache itself, where bootsnap and the asset caches live too, and
+ # CacheCleanupJob (FileStore#cleanup) reads every file below the store's
+ # directory. In every environment of production.rb, the image included.
+ describe 'the cache store' do
+ it 'is a file store in tmp/cache/store, with or without LALABASE_HOST' do
+ [evaluate, evaluate('LALABASE_HOST' => 'tickets.example.org')].each do |config|
+ expect(config[:cache_store]).to eq([:file_store, Rails.root.join('tmp/cache/store')])
+ end
+ end
+ end
+
# Lines a shared .env may carry from an old template. production.rb used to
# ignore them, and it still must.
context 'on our servers with ASSET_HOST, RAILS_LOG_TO_STDOUT and LALABASE_FORCE_SSL lines' do
diff --git a/spec/config/recurring_schedule_spec.rb b/spec/config/recurring_schedule_spec.rb
index d7492e0..a039afe 100644
--- a/spec/config/recurring_schedule_spec.rb
+++ b/spec/config/recurring_schedule_spec.rb
@@ -29,6 +29,14 @@ RSpec.describe 'config/recurring.yml' do
end
end
+ # Without a sweep the file store keeps every expired entry for good.
+ it 'contains the hourly cache cleanup' do
+ entry = schedule.fetch(env)['cache_cleanup']
+ expect(entry).to be_present
+ expect(entry['class']).to eq('CacheCleanupJob')
+ expect(Fugit.parse(entry['schedule'])).to be_present
+ end
+
it 'contains the documentation polling sync (Slice-2 auto-sync contract)' do
entry = schedule.fetch(env)['documentation_polling_sync']
expect(entry).to be_present
diff --git a/spec/jobs/cache_cleanup_job_spec.rb b/spec/jobs/cache_cleanup_job_spec.rb
new file mode 100644
index 0000000..1c2fd18
--- /dev/null
+++ b/spec/jobs/cache_cleanup_job_spec.rb
@@ -0,0 +1,59 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+
+# What: the hourly sweep of expired cache entries (config/recurring.yml).
+# Type: Job spec against a real file store in a temporary directory: the
+# point is which files stay on the disk, and a double has no disk.
+# Why: Production caches in a file store, and a file store deletes nothing on
+# its own. An expired entry (a rate limit counter, which until 2026-10-31
+# named the raw access token in its file name) stays on the disk for good
+# unless this job removes it.
+RSpec.describe CacheCleanupJob do
+ include ActiveSupport::Testing::TimeHelpers
+
+ around do |example|
+ Dir.mktmpdir do |dir|
+ @dir = dir
+ example.run
+ end
+ end
+
+ let(:store) { ActiveSupport::Cache::FileStore.new(File.join(@dir, 'store')) }
+
+ before { allow(Rails).to receive(:cache).and_return(store) }
+
+ it 'deletes expired entries and keeps live ones' do
+ store.write('expired', 1, expires_in: 1.minute)
+ store.write('live', 2, expires_in: 1.hour)
+ store.write('forever', 3)
+
+ travel 2.minutes do
+ described_class.perform_now
+
+ expect(store.read('live')).to eq(2)
+ expect(store.read('forever')).to eq(3)
+ end
+ expect(Dir.glob(File.join(@dir, 'store', '**', '*')).select { |f| File.file?(f) }.size).to eq(2)
+ end
+
+ # The production store has a directory of its own under tmp/cache
+ # (production.rb). This pins what that is for: a file the store did not
+ # write survives, so bootsnap and the asset caches next to it would as well.
+ it 'leaves a file it did not write alone' do
+ foreign = File.join(@dir, 'store', 'foreign.bin')
+ FileUtils.mkdir_p(File.dirname(foreign))
+ File.binwrite(foreign, "\x04\x08not an entry")
+
+ described_class.perform_now
+
+ expect(File.exist?(foreign)).to be(true)
+ end
+
+ # Redis and Memcache expire entries themselves and raise on cleanup.
+ it 'does nothing on a store that cannot clean up' do
+ allow(Rails).to receive(:cache).and_return(Class.new(ActiveSupport::Cache::Store).new)
+
+ expect { described_class.perform_now }.not_to raise_error
+ end
+end