Class: Phlex::FIFO Private

Inherits:
Object
  • Object
show all
Defined in:
lib/phlex/fifo.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_bytesize: 2_000, max_value_bytesize: 2_000) ⇒ FIFO

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of FIFO.



5
6
7
8
9
10
11
# File 'lib/phlex/fifo.rb', line 5

def initialize(max_bytesize: 2_000, max_value_bytesize: 2_000)
	@store = {}
	@max_bytesize = max_bytesize
	@max_value_bytesize = max_value_bytesize
	@bytesize = 0
	@mutex = Monitor.new
end

Instance Attribute Details

#bytesizeObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



13
14
15
# File 'lib/phlex/fifo.rb', line 13

def bytesize
  @bytesize
end

#max_bytesizeObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



13
14
15
# File 'lib/phlex/fifo.rb', line 13

def max_bytesize
  @max_bytesize
end

Instance Method Details

#[](key) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



21
22
23
24
# File 'lib/phlex/fifo.rb', line 21

def [](key)
	k, v = @store[key.hash]
	v if k.eql?(key)
end

#[]=(key, value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/phlex/fifo.rb', line 26

def []=(key, value)
	return if value.bytesize > @max_value_bytesize

	digest = key.hash

	@mutex.synchronize do
		# Check the key definitely doesn't exist now we have the lock
		return if @store[digest]

		@store[digest] = [key, value].freeze
		@bytesize += value.bytesize

		while @bytesize > @max_bytesize
			_k, v = @store.shift
			@bytesize -= v[1].bytesize
		end
	end
end

#clearObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



49
50
51
52
53
54
# File 'lib/phlex/fifo.rb', line 49

def clear
	@mutex.synchronize do
		@store.clear
		@bytesize = 0
	end
end

#expand(bytes) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



15
16
17
18
19
# File 'lib/phlex/fifo.rb', line 15

def expand(bytes)
	@mutex.synchronize do
		@max_bytesize += bytes
	end
end

#sizeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



45
46
47
# File 'lib/phlex/fifo.rb', line 45

def size
	@store.size
end