lib/openwfe/util/lru.rb in openwferu-0.9.15 vs lib/openwfe/util/lru.rb in openwferu-0.9.16
- old
+ new
@@ -62,91 +62,110 @@
@hash = {}
@lru_keys = []
end
def maxsize= (newsize)
+
remove_lru() while @hash.size > newsize
@maxsize = newsize
end
def maxsize
- return @maxsize
+
+ @maxsize
end
def size
- return @lru_keys.size
+ @lru_keys.size
end
alias :length :size
def keys
- return @hash.keys
+
+ @hash.keys
end
+
def values
- return @values
+
+ @hash.values
end
def clear
+
@hash.clear
@lru_keys.clear
end
def each (&block)
+
return unless block
+
@hash.each do |k, v|
block.call(k, v)
end
end
#
# Returns the keys with the lru in front.
#
def ordered_keys
- return @lru_keys
+
+ @lru_keys
end
def [] (key)
+
value = @hash[key]
return nil unless value
touch(key)
- return value
+
+ value
end
def []= (key, value)
+
remove_lru() while @hash.size >= @maxsize
@hash[key] = value
touch(key)
- return value
+
+ value
end
def delete (key)
+
value = @hash.delete(key)
@lru_keys.delete(key)
- return value
+
+ value
end
def include? (key)
- return @hash.include?(key)
+
+ @hash.include?(key)
end
protected
#
# Puts the key on top of the lru 'stack'.
# The bottom being the lru place.
#
def touch (key)
+
@lru_keys.delete(key)
@lru_keys << key
end
#
# Removes the lru value and returns it.
# Returns nil if the cache is currently empty.
#
def remove_lru ()
+
return nil if @lru_keys.size < 1
key = @lru_keys.delete_at(0)
- return @hash.delete(key)
+
+ @hash.delete(key)
end
end
end