-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
123 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
require 'redis' | ||
|
||
class RedisCache | ||
def initialize(config:) | ||
@config = config | ||
end | ||
attr_reader :config | ||
|
||
def flush | ||
unless redis | ||
config.logger.warn 'REDIS_URL not set, skipping Redis cache flush' | ||
return | ||
end | ||
|
||
result = redis.flushall | ||
if result == 'OK' | ||
config.logger.info 'Redis cache flushed' | ||
else | ||
config.logger.error "Flushing Redis cache failed: #{result}" | ||
end | ||
rescue StandardError => e | ||
config.logger.error "Flushing Redis cache failed: #{e.message}" | ||
end | ||
|
||
def redis | ||
return unless config.redis_url | ||
|
||
@redis ||= Redis.new(url: config.redis_url) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
require 'redis_cache' | ||
|
||
describe RedisCache do | ||
subject(:redis_cache) { described_class.new(config:) } | ||
|
||
let(:config) { Config.new(ENV.to_h, logger:) } | ||
let(:logger) { MemoryLogger.new } | ||
|
||
describe '#flush' do | ||
subject(:flush) { redis_cache.flush } | ||
|
||
context 'when Redis is available' do | ||
it 'writes info message into log' do | ||
flush | ||
|
||
expect(logger.info_messages).to include('Redis cache flushed') | ||
end | ||
end | ||
|
||
context 'when Redis is not available' do | ||
before do | ||
allow(config).to receive(:redis_url).and_return( | ||
'redis://localhost:1234', | ||
) | ||
end | ||
|
||
it 'writes error message into log' do | ||
flush | ||
|
||
expect(logger.error_messages).to include( | ||
'Flushing Redis cache failed: Connection refused - connect(2) for 127.0.0.1:1234 (redis://localhost:1234)', | ||
) | ||
end | ||
end | ||
|
||
context 'when Redis cannot flush' do | ||
before do | ||
allow(Redis).to receive(:new).and_return( | ||
instance_double(Redis, flushall: 'ERROR'), | ||
) | ||
end | ||
|
||
it 'writes error message into log' do | ||
flush | ||
|
||
expect(logger.error_messages).to include( | ||
'Flushing Redis cache failed: ERROR', | ||
) | ||
end | ||
end | ||
|
||
context 'when REDIS_URL missing' do | ||
before { allow(config).to receive(:redis_url).and_return(nil) } | ||
|
||
it 'writes warn message into log' do | ||
flush | ||
|
||
expect(logger.warn_messages).to include( | ||
'REDIS_URL not set, skipping Redis cache flush', | ||
) | ||
end | ||
end | ||
end | ||
end |