Skip to content

Instantly share code, notes, and snippets.

@Eric-Guo
Forked from Skulli/exception_notification.rb
Created November 8, 2016 02:57
Show Gist options
  • Save Eric-Guo/9374e8ef663d0704550916c78e247701 to your computer and use it in GitHub Desktop.
Save Eric-Guo/9374e8ef663d0704550916c78e247701 to your computer and use it in GitHub Desktop.
config.ignore_if do |exception, options|
# use beginning to prevent non unique keys (they include object ids and such)
key = exception.message.split("for").first[0..30]
ExpirableKey.new(key).exist_with_renew
end
class ExpirableKey
attr_accessor :key, :redis, :expire_in
def initialize(key, connection: Redis.current, expire_in: 60)
self.redis = connection
self.expire_in = expire_in
self.key = key
end
def exist?
redis.get(key).present?
end
def empty?
not(exist?)
end
def exist_with_renew?(value: true)
result = exist?
set(value)
result
end
def set(value = true)
redis.set(key, value)
redis.expire(key, expire_in) if expire_in.present?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment