Skip to content

Commit

Permalink
LRUCache -> Cache for simplicity
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcwatt committed Aug 25, 2024
1 parent 411d7e0 commit d243872
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions lib/http/headers/normalizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Normalizer
MAX_CACHE_SIZE = 200

def initialize
@cache = LRUCache.new(MAX_CACHE_SIZE)
@cache = Cache.new(MAX_CACHE_SIZE)
end

# Transforms `name` to canonical HTTP header capitalization
Expand All @@ -39,31 +39,24 @@ def normalize_header(name)
raise HeaderError, "Invalid HTTP header field name: #{name.inspect}"
end

class LRUCache
class Cache
def initialize(max_size)
@max_size = max_size
@cache = {}
@order = []
end

def get(key)
return unless @cache.key?(key)

# Move the accessed item to the end of the order array
@order.delete(key)
@order.push(key)
@cache[key]
end

def set(key, value)
@cache[key] = value
@order.push(key)

# Maintain cache size
return unless @order.size > @max_size
return unless @cache.size > @max_size

oldest = @order.shift
@cache.delete(oldest)
oldest_key = @cache.keys.first
@cache.delete(oldest_key)
end

def size
Expand All @@ -83,7 +76,7 @@ def []=(key, value)
end
end

private_constant :LRUCache
private_constant :Cache
end
end
end

0 comments on commit d243872

Please sign in to comment.