module RedisLike::Helpers::Lists
Public Instance Methods
block_while_empty(list, interval = 0.010)
click to toggle source
# File lib/redislike/helpers/lists.rb, line 39 def block_while_empty(list, interval = 0.010) sleep interval until llen(list) > 0 end
dequeue(list, from:)
click to toggle source
# File lib/redislike/helpers/lists.rb, line 12 def dequeue(list, from:) operation = from == :head ? :shift : :pop current = fetch list, [] item = current.method(operation).call store list, current item end
enqueue(list, item, to:, allow_create: true)
click to toggle source
# File lib/redislike/helpers/lists.rb, line 4 def enqueue(list, item, to:, allow_create: true) operation = to == :head ? :unshift : :push items = fetch list, [] items.method(operation).call(item) store list, items if allow_create || exists(list) llen list end
remove_all(list, item)
click to toggle source
# File lib/redislike/helpers/lists.rb, line 31 def remove_all(list, item) items = fetch list, [] remove_count list, items.count(item), item, from: :head # remaining = items.reject { |i| i == item } # store list, remaining # items.length - remaining.length end
remove_count(list, count, item, from:)
click to toggle source
# File lib/redislike/helpers/lists.rb, line 20 def remove_count(list, count, item, from:) operation = from == :tail ? :rindex : :index items = fetch list, [] removed = count.times.map do found = items.method(operation).call(item) items.delete_at found if found end store list, items removed.compact.length end