This repository has been archived by the owner on May 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from Sage/feature_optional_redis_cache
OptionalRedisCacheStore
- Loading branch information
Showing
9 changed files
with
403 additions
and
220 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
require spec_helper | ||
--color | ||
--require spec_helper | ||
--format doc |
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
97 changes: 97 additions & 0 deletions
97
cache_store_redis/lib/cache_store_redis/optional_redis_cache_store.rb
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,97 @@ | ||
require 'logger' | ||
|
||
# This class is used to define a redis cache store that logs failures as warnings but does not raise errors for | ||
# cache connections | ||
class OptionalRedisCacheStore | ||
def initialize(namespace: nil, config: nil, logger: nil) | ||
@cache_store = RedisCacheStore.new(namespace, config) | ||
@logger = logger || Logger.new(STDOUT) | ||
end | ||
|
||
def redis_store | ||
@cache_store | ||
end | ||
|
||
# This method is called to configure the connection to the cache store. | ||
def configure( | ||
host = 'localhost', | ||
port = 6379, | ||
db = 'default', | ||
password = nil, | ||
driver: nil, | ||
url: nil, | ||
connect_timeout: 0.5, | ||
read_timeout: 1, | ||
write_timeout: 0.5) | ||
redis_store.configure( | ||
host, | ||
port, | ||
db, | ||
password, | ||
driver: driver, | ||
url: url, | ||
connect_timeout: connect_timeout, | ||
read_timeout: read_timeout, | ||
write_timeout: write_timeout | ||
) | ||
end | ||
|
||
def optional_get(key, expires_in = 0) | ||
redis_store.get(key, expires_in) | ||
rescue => e | ||
@logger.error( | ||
"[#{self.class}] - An error occurred requesting data from the cache. " \ | ||
"Key: #{key} | Error: #{e.message} | Backtrace: #{e.backtrace}" | ||
) | ||
nil | ||
end | ||
|
||
def get(key, expires_in = 0, &block) | ||
value = optional_get(key, expires_in) | ||
|
||
if value.nil? && block_given? | ||
value = yield | ||
set(key, value, expires_in) | ||
end | ||
|
||
value | ||
end | ||
|
||
def set(key, value, expires_in = 0) | ||
redis_store.set(key, value, expires_in) | ||
rescue => e | ||
@logger.error( | ||
"[#{self.class}] - An error occurred storing data in the cache. " \ | ||
"Key: #{key} | Error: #{e.message} | Backtrace: #{e.backtrace}" | ||
) | ||
end | ||
|
||
def remove(key) | ||
redis_store.remove(key) | ||
rescue => e | ||
@logger.error( | ||
"[#{self.class}] - An error occurred removing data from the cache. " \ | ||
"Key: #{key} | Error: #{e.message} | Backtrace: #{e.backtrace}" | ||
) | ||
end | ||
|
||
def exist?(key) | ||
redis_store.exist?(key) | ||
rescue => e | ||
@logger.error( | ||
"[#{self.class}] - An error occurred checking if a key exists in the cache. " \ | ||
"Key: #{key} | Error: #{e.message} | Backtrace: #{e.backtrace}" | ||
) | ||
false | ||
end | ||
|
||
def ping | ||
redis_store.ping | ||
rescue => e | ||
@logger.error( | ||
"[#{self.class}] - An error occurred checking pinging the cache. " \ | ||
"Error: #{e.message} | Backtrace: #{e.backtrace}" | ||
) | ||
false | ||
end | ||
end |
Oops, something went wrong.