From fdb0b7f9d5b6aa823bd3a09e96600b2bf30aef60 Mon Sep 17 00:00:00 2001 From: Ian Remillard Date: Fri, 28 Sep 2018 12:43:52 -0700 Subject: [PATCH] adds lock ttl and lock_release_removes_key --- lib/suo/client/base.rb | 11 +++++++++-- lib/suo/client/memcached.rb | 6 +++++- lib/suo/client/redis.rb | 6 +++++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/suo/client/base.rb b/lib/suo/client/base.rb index 7f37355..3427a20 100644 --- a/lib/suo/client/base.rb +++ b/lib/suo/client/base.rb @@ -5,7 +5,9 @@ module Suo acquisition_timeout: 0.1, acquisition_delay: 0.01, stale_lock_expiration: 3600, - resources: 1 + resources: 1, + lock_release_removes_key: false, + ttl: nil, }.freeze BLANK_STR = "".freeze @@ -81,7 +83,12 @@ module Suo acquisition_lock = remove_lock(cleared_locks, token) break unless acquisition_lock - break if set(serialize_locks(cleared_locks), cas) + + if @options[:lock_release_removes_key] && cleared_locks.empty? + break if clear + else + break if set(serialize_locks(cleared_locks), cas) + end end rescue LockClientError => _ # rubocop:disable Lint/HandleExceptions # ignore - assume success due to optimistic locking diff --git a/lib/suo/client/memcached.rb b/lib/suo/client/memcached.rb index 83b37b6..3e15d75 100644 --- a/lib/suo/client/memcached.rb +++ b/lib/suo/client/memcached.rb @@ -17,7 +17,11 @@ module Suo end def set(newval, cas) - @client.set_cas(@key, newval, cas) + if @options[:ttl] + @client.set_cas(@key, newval, cas, {ttl: @options[:ttl]}) + else + @client.set_cas(@key, newval, cas) + end end def initial_set(val = BLANK_STR) diff --git a/lib/suo/client/redis.rb b/lib/suo/client/redis.rb index b13119e..ff736e5 100644 --- a/lib/suo/client/redis.rb +++ b/lib/suo/client/redis.rb @@ -20,7 +20,11 @@ module Suo def set(newval, _) ret = @client.multi do |multi| - multi.set(@key, newval) + if @options[:ttl] + multi.setex(@key, @options[:ttl], newval) + else + multi.set(@key, newval) + end end ret && ret[0] == OK_STR