lib/rufus/lru.rb in rufus-lru-1.0.1 vs lib/rufus/lru.rb in rufus-lru-1.0.2
- old
+ new
@@ -73,11 +73,11 @@
end
def maxsize= (s)
@maxsize = s
- remove_lru while size > @maxsize
+ remove_lru
end
def clear
super
@@ -94,28 +94,35 @@
def [] (key)
value = super
return nil unless value
- touch(key)
+ touch key
value
end
def []= (key, value)
- remove_lru while size >= @maxsize
+ remove_lru
super
- touch(key)
+ touch key
value
end
+ def merge! (hash)
+
+ hash.each { |k, v| self[k] = v }
+
+ # not using 'super', but in order not guaranteed at all...
+ end
+
def delete (key)
value = super
- @lru_keys.delete(key)
+ @lru_keys.delete key
value
end
protected
@@ -124,20 +131,23 @@
# Puts the key on top of the lru 'stack'.
# The bottom being the lru place.
#
def touch (key)
- @lru_keys.delete(key)
+ @lru_keys.delete key
@lru_keys << key
end
#
- # Removes the lru value and returns it.
- # Returns nil if the cache is currently empty.
+ # Makes sure that the hash fits its maxsize. If not, will remove
+ # the least recently used items.
#
def remove_lru
- key = @lru_keys.delete_at 0
- delete key
+ while size >= @maxsize
+
+ key = @lru_keys.delete_at 0
+ delete key
+ end
end
end