lib/once.rb in once-0.0.1 vs lib/once.rb in once-0.0.3
- old
+ new
@@ -11,11 +11,11 @@
# 2. Use to wrap a call that you want done uniquely
# Once.do(name: "sending_email", params: { email: "foo@bar.com" }, within: 1.hour) do
# .. stuff that should happen only once ..
# end
#
-# The combination of the name and params makes the check unique. So typically it would be the
+# The combination of the name and params makes the check unique. So typically it would be the
# command you're executing, plus the params to that command
module Once
DEFAULT_TIME = 3600 # seconds
class << self
@@ -35,14 +35,19 @@
# params: The params that will control whether or not the body executes
def do(name:, params:, within: DEFAULT_TIME, &block)
hash = Digest::MD5.hexdigest(params.inspect)
redis_key = "uniquecheck:#{name}:#{hash}"
+ perform(redis_key, &block).tap do
+ redis.setex(redis_key, within, true)
+ end
+ end
+
+ private
+
+ def perform(redis_key, &block)
unless redis.exists(redis_key)
block.call
end
-
- redis.set(redis_key, true)
- redis.expire(redis_key, within)
end
end
end