diff --git a/README.md b/README.md index 7136f54..4a3923f 100644 --- a/README.md +++ b/README.md @@ -75,18 +75,11 @@ end ### Time To Live ```ruby -Suo::Client::Redis.new("bar_resource", ttl: 10) #ttl in seconds +Suo::Client::Redis.new("bar_resource", ttl: 60) #ttl in seconds ``` -All locks are expired and the lock key is removed after the specified `ttl` time runs out. This is counted as the time since any resource for a given key was locked or unlocked. +A key representing a set of lockable resources is removed once the last resource lock is released and the `ttl` time runs out. When another lock is acquired and the key has been removed the key has to be recreated. -### Lock Release Removes Key - -```ruby -Suo::Client::Redis.new("bar_resource", lock_release_removes_key: true) -``` - -By default, a key representing a set of resource locks is persisted indefinitely even when no resources are currently locked. When `lock_release_removes_key` is set to `true`, the key is removed when the last resource lock is released. This also means that when another lock is acquired the key has to be recreated. ## TODO - more race condition tests diff --git a/lib/suo/client/base.rb b/lib/suo/client/base.rb index 3427a20..c439575 100644 --- a/lib/suo/client/base.rb +++ b/lib/suo/client/base.rb @@ -6,8 +6,7 @@ module Suo acquisition_delay: 0.01, stale_lock_expiration: 3600, resources: 1, - lock_release_removes_key: false, - ttl: nil, + ttl: 60, }.freeze BLANK_STR = "".freeze @@ -66,7 +65,7 @@ module Suo refresh_lock(cleared_locks, token) - break if set(serialize_locks(cleared_locks), cas) + break if set(serialize_locks(cleared_locks), cas, expire: cleared_locks.empty?) end end @@ -83,12 +82,7 @@ module Suo acquisition_lock = remove_lock(cleared_locks, token) break unless acquisition_lock - - if @options[:lock_release_removes_key] && cleared_locks.empty? - break if clear - else - break if set(serialize_locks(cleared_locks), cas) - end + break if set(serialize_locks(cleared_locks), cas, expire: cleared_locks.empty?) 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 3e15d75..2aabc8a 100644 --- a/lib/suo/client/memcached.rb +++ b/lib/suo/client/memcached.rb @@ -16,9 +16,9 @@ module Suo @client.get_cas(@key) end - def set(newval, cas) - if @options[:ttl] - @client.set_cas(@key, newval, cas, {ttl: @options[:ttl]}) + def set(newval, cas, expire:) + if expire + @client.set_cas(@key, newval, cas, @options[:ttl]) else @client.set_cas(@key, newval, cas) end diff --git a/lib/suo/client/redis.rb b/lib/suo/client/redis.rb index ff736e5..98861f9 100644 --- a/lib/suo/client/redis.rb +++ b/lib/suo/client/redis.rb @@ -18,9 +18,9 @@ module Suo [@client.get(@key), nil] end - def set(newval, _) + def set(newval, _, expire:) ret = @client.multi do |multi| - if @options[:ttl] + if expire multi.setex(@key, @options[:ttl], newval) else multi.set(@key, newval)