Skip to content

Instantly share code, notes, and snippets.

@Skulli
Last active March 15, 2018 16:19
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Skulli/80a3f31d1b25b7b98c025b2bd182aa4a to your computer and use it in GitHub Desktop.
Save Skulli/80a3f31d1b25b7b98c025b2bd182aa4a 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