Class: Threatstack::Utils::CappedQueue
- Inherits:
-
Object
- Object
- Threatstack::Utils::CappedQueue
- Defined in:
- lib/utils/capped_queue.rb
Instance Method Summary collapse
- #clear ⇒ Object
-
#initialize(max) ⇒ CappedQueue
constructor
A new instance of CappedQueue.
- #length ⇒ Object
- #push(*args) ⇒ Object
- #shift(*args) ⇒ Object
Constructor Details
#initialize(max) ⇒ CappedQueue
Returns a new instance of CappedQueue.
9 10 11 12 13 |
# File 'lib/utils/capped_queue.rb', line 9 def initialize(max) @queue = [] @max = max @mutex = Mutex.new end |
Instance Method Details
#clear ⇒ Object
32 33 34 35 36 |
# File 'lib/utils/capped_queue.rb', line 32 def clear @mutex.synchronize do @queue.clear end end |
#length ⇒ Object
15 16 17 |
# File 'lib/utils/capped_queue.rb', line 15 def length @queue.length end |
#push(*args) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/utils/capped_queue.rb', line 19 def push(*args) @mutex.synchronize do @queue.push(*args) @queue.shift(@queue.length - @max) if @queue.length > @max end end |
#shift(*args) ⇒ Object
26 27 28 29 30 |
# File 'lib/utils/capped_queue.rb', line 26 def shift(*args) @mutex.synchronize do @queue.shift(*args) end end |