Skip to content

Commit

Permalink
Improve naming in FIFO
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldrapper committed Sep 4, 2024
1 parent 262f006 commit d5c839f
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions lib/phlex/fifo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class Phlex::FIFO
def initialize(max_bytesize = 2_000, max_value_bytesize = 2_000)
@hash = {}
@store = {}
@max_bytesize = max_bytesize
@max_value_bytesize = max_value_bytesize
@bytesize = 0
Expand All @@ -18,36 +18,34 @@ def expand(bytes)
end

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

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

hash = key.hash
digest = key.hash

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

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

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

def size
@hash.size
@store.size
end

def size
@hash.size
@store.size
end
end

0 comments on commit d5c839f

Please sign in to comment.