lib/redis_token.rb in redis_token-0.0.3 vs lib/redis_token.rb in redis_token-0.0.4

- old
+ new

@@ -117,26 +117,11 @@ # # @param [String] owner # # @return [Enumerator] def owned_by(owner) - mask = "#{@prefix}#{owner}.*" - - Enumerator.new do |y| - cursor = 0 - loop do - cursor, r = @redis.scan(cursor, match: mask) - cursor = cursor.to_i - - r.each do |key| - token = owner_key_to_token(owner, key) - y << [token, redis_get(token_to_key(token))] - end - - break if cursor == 0 - end - end + owned_tokens(owner).map { |token| [token, redis_get(token_to_key(token))]} end # Delete a token # # @param [String] token @@ -153,10 +138,29 @@ end true end + alias delete del + + # Delete all tokens of an owner + # + # @params [String] owner + # + # @return [Integer] number of deleted tokens + def del_all(owner) + deleted = 0 + owned_tokens(owner).each do |token| + del(token) + deleted += 1 + end + + deleted + end + + alias delete_all del_all + # Retrieve the remaining ttl of a token # # @return [Integer] ttl def ttl(token) @redis.ttl(token_to_key(token)) @@ -187,7 +191,26 @@ def redis_get(key) value = @redis.get(key) return unless value Marshal.load(value) + end + + def owned_tokens(owner) + mask = "#{@prefix}#{owner}.*" + + Enumerator.new do |y| + cursor = 0 + loop do + cursor, r = @redis.scan(cursor, match: mask) + cursor = cursor.to_i + + r.each do |key| + token = owner_key_to_token(owner, key) + y << token + end + + break if cursor == 0 + end + end end end