Просмотр исходного кода

Maintenance: CacheClearJob cleans up cache only if using FileStore

Mantas Masalskis 2 лет назад
Родитель
Сommit
7d947e51c4
2 измененных файлов с 41 добавлено и 3 удалено
  1. 4 3
      app/jobs/cache_clear_job.rb
  2. 37 0
      spec/jobs/cache_clear_job_spec.rb

+ 4 - 3
app/jobs/cache_clear_job.rb

@@ -4,9 +4,10 @@ class CacheClearJob < ApplicationJob
   include HasActiveJobLock
 
   def perform
-    # cleanup is not supported by every backend so
-    # try only if exists
-    Rails.cache.try(:cleanup)
+    # Memcached does not support clean-up, so only perform it for filesystem cache.
+    return if !Rails.cache.is_a? ActiveSupport::Cache::FileStore
+
+    Rails.cache.cleanup
   rescue => e
     Rails.logger.error "Scheduled cache cleanup failed! #{e.inspect}"
   end

+ 37 - 0
spec/jobs/cache_clear_job_spec.rb

@@ -0,0 +1,37 @@
+# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
+
+require 'rails_helper'
+
+RSpec.describe CacheClearJob do
+  around do |example|
+    old_cache = Rails.cache
+
+    Rails.cache = cache
+
+    example.run
+  ensure
+    Rails.cache = old_cache
+  end
+
+  before do
+    allow(Rails.cache).to receive(:cleanup).and_call_original
+  end
+
+  context 'when Cache is FileStore' do
+    let(:cache) { ActiveSupport::Cache::FileStore.new 'path' }
+
+    it 'does cleanup' do
+      described_class.perform_now
+      expect(Rails.cache).to have_received :cleanup
+    end
+  end
+
+  context 'when Cache is Memcached' do
+    let(:cache) { ActiveSupport::Cache::MemCacheStore.new }
+
+    it 'does not cleanup' do
+      described_class.perform_now
+      expect(Rails.cache).not_to have_received :cleanup
+    end
+  end
+end