-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a
run: "2 times per day"
option to rate limit some checks (exam…
…ple: expensive API calls)
- Loading branch information
Showing
7 changed files
with
232 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# frozen_string_literal: true | ||
|
||
module Allgood | ||
class CacheStore | ||
def self.instance | ||
@instance ||= new | ||
end | ||
|
||
def initialize | ||
@memory_store = {} | ||
end | ||
|
||
def read(key) | ||
if rails_cache_available? | ||
Rails.cache.read(key) | ||
else | ||
@memory_store[key] | ||
end | ||
end | ||
|
||
def write(key, value) | ||
if rails_cache_available? | ||
expiry = key.include?('day') ? 1.day : 1.hour | ||
Rails.cache.write(key, value, expires_in: expiry) | ||
else | ||
@memory_store[key] = value | ||
end | ||
end | ||
|
||
def cleanup_old_keys | ||
return unless rails_cache_available? | ||
|
||
keys_pattern = "allgood:*" | ||
if Rails.cache.respond_to?(:delete_matched) | ||
Rails.cache.delete_matched("#{keys_pattern}:*:#{(Time.current - 2.days).strftime('%Y-%m-%d')}*") | ||
end | ||
rescue StandardError => e | ||
Rails.logger.warn "Allgood: Failed to cleanup old cache keys: #{e.message}" | ||
end | ||
|
||
private | ||
|
||
def rails_cache_available? | ||
Rails.cache && Rails.cache.respond_to?(:read) && Rails.cache.respond_to?(:write) && | ||
Rails.cache.write("allgood_rails_cache_test_ok", "true") && | ||
Rails.cache.read("allgood_rails_cache_test_ok") == "true" | ||
rescue StandardError => e | ||
Rails.logger.warn "Allgood: Rails.cache not available (#{e.message}), falling back to memory store" | ||
false | ||
end | ||
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
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,5 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
module Allgood | ||
VERSION = "0.2.0" | ||
VERSION = "0.3.0" | ||
end |