Handle cache invalidation events on Geo secondary

Expires the cache of the given key when the secondary node
gets a Geo::CacheInvalidationEvent.
parent 63967d47
# frozen_string_literal: true
module Gitlab
module Geo
module LogCursor
module Events
class CacheInvalidationEvent
include BaseEvent
def process
result = expire_cache_for_event_key
log_cache_invalidation_event(result)
end
private
def expire_cache_for_event_key
Rails.cache.delete(event.key)
end
def log_cache_invalidation_event(expired)
logger.event_info(
created_at,
'Cache invalidation',
cache_key: event.key,
cache_expired: expired,
skippable: skippable?
)
end
def skippable?
false
end
end
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
describe Gitlab::Geo::LogCursor::Events::CacheInvalidationEvent, :postgresql, :clean_gitlab_redis_shared_state do
let(:logger) { Gitlab::Geo::LogCursor::Logger.new(described_class, Logger::INFO) }
let(:event_log) { create(:geo_event_log, :cache_invalidation_event) }
let!(:event_log_state) { create(:geo_event_log_state, event_id: event_log.id - 1) }
let(:cache_invalidation_event) { event_log.cache_invalidation_event }
let(:cache_key) { cache_invalidation_event.key }
subject { described_class.new(cache_invalidation_event, Time.now, logger) }
around do |example|
Sidekiq::Testing.fake! { example.run }
end
describe '#process' do
it 'expires the cache of the given key' do
expect(Rails.cache).to receive(:delete).with(cache_key).and_call_original
subject.process
end
it 'logs an info event' do
data = {
class: described_class.name,
message: 'Cache invalidation',
cache_key: cache_key,
cache_expired: false,
skippable: false
}
expect(::Gitlab::Logger)
.to receive(:info)
.with(hash_including(data))
subject.process
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment