refactor: avoid rescuing pattern repetition

This commit is contained in:
Gonzalo Rodriguez 2019-02-28 22:51:57 -03:00
parent 20d668211e
commit 04eeeb9a33
No known key found for this signature in database
GPG key ID: 5DB8B81B049B8AB1
3 changed files with 40 additions and 36 deletions

View file

@ -24,35 +24,35 @@ module Rack
end end
def read(key) def read(key)
with do |client| rescuing do
client.get(key) with do |client|
client.get(key)
end
end end
rescue Dalli::DalliError
nil
end end
def write(key, value, options = {}) def write(key, value, options = {})
with do |client| rescuing do
client.set(key, value, options.fetch(:expires_in, 0), raw: true) with do |client|
client.set(key, value, options.fetch(:expires_in, 0), raw: true)
end
end end
rescue Dalli::DalliError
nil
end end
def increment(key, amount, options = {}) def increment(key, amount, options = {})
with do |client| rescuing do
client.incr(key, amount, options.fetch(:expires_in, 0), amount) with do |client|
client.incr(key, amount, options.fetch(:expires_in, 0), amount)
end
end end
rescue Dalli::DalliError
nil
end end
def delete(key) def delete(key)
with do |client| rescuing do
client.delete(key) with do |client|
client.delete(key)
end
end end
rescue Dalli::DalliError
nil
end end
private private
@ -64,6 +64,12 @@ module Rack
end end
end end
end end
def rescuing
yield
rescue Dalli::DalliError
nil
end
end end
end end
end end

View file

@ -19,36 +19,38 @@ module Rack
end end
def read(key) def read(key)
get(key) rescuing { get(key) }
rescue Redis::BaseError
nil
end end
def write(key, value, options = {}) def write(key, value, options = {})
if (expires_in = options[:expires_in]) if (expires_in = options[:expires_in])
setex(key, expires_in, value) rescuing { setex(key, expires_in, value) }
else else
set(key, value) rescuing { set(key, value) }
end end
rescue Redis::BaseError
nil
end end
def increment(key, amount, options = {}) def increment(key, amount, options = {})
count = nil count = nil
pipelined do rescuing do
count = incrby(key, amount) pipelined do
expire(key, options[:expires_in]) if options[:expires_in] count = incrby(key, amount)
expire(key, options[:expires_in]) if options[:expires_in]
end
end end
count.value if count count.value if count
rescue Redis::BaseError
nil
end end
def delete(key, _options = {}) def delete(key, _options = {})
del(key) rescuing { del(key) }
end
private
def rescuing
yield
rescue Redis::BaseError rescue Redis::BaseError
nil nil
end end

View file

@ -11,19 +11,15 @@ module Rack
end end
def read(key) def read(key)
get(key, raw: true) rescuing { get(key, raw: true) }
rescue Redis::BaseError
nil
end end
def write(key, value, options = {}) def write(key, value, options = {})
if (expires_in = options[:expires_in]) if (expires_in = options[:expires_in])
setex(key, expires_in, value, raw: true) rescuing { setex(key, expires_in, value, raw: true) }
else else
set(key, value, raw: true) rescuing { set(key, value, raw: true) }
end end
rescue Redis::BaseError
nil
end end
end end
end end