lib/perobs/PersistentObjectCache.rb in perobs-4.1.0 vs lib/perobs/PersistentObjectCache.rb in perobs-4.2.0
- old
+ new
@@ -42,11 +42,12 @@
# the get() method to prevent duplicate instances in memory of the same
# in-store object. The cache uses a least-recently-used (LRU) scheme to
# cache objects.
# @param size [Integer] Minimum number of objects to be cached at a time
# @param flush_delay [Integer] Determines how often non-forced flushes are
- # ignored in a row before the flush is really done.
+ # ignored in a row before the flush is really done. If flush_delay
+ # is smaller than 0 non-forced flushed will always be ignored.
# @param klass [Class] The class of the objects to be cached. Objects must
# provide a uid() method that returns a unique ID for every object.
# @param collection [] The object collection the objects belong to. It
# must provide a ::load method.
def initialize(size, flush_delay, klass, collection)
@@ -69,12 +70,11 @@
end
if modified
@modified_entries[object.uid] = object
else
- index = object.uid % @size
- @unmodified_entries[index] = object
+ @unmodified_entries[object.uid % @size] = object
end
nil
end
@@ -109,12 +109,15 @@
# Write all excess modified objects into the backing store. If now is true
# all modified objects will be written.
# @param now [Boolean]
def flush(now = false)
- if now || (@flush_counter -= 1) <= 0
+ if now || (@flush_delay >= 0 && (@flush_counter -= 1) <= 0)
@modified_entries.each do |id, object|
object.save
+ # Add the object to the unmodified object cache. We might still need
+ # it again soon.
+ @unmodified_entries[object.uid % @size] = object
end
@modified_entries = ::Hash.new
@flush_counter = @flush_delay
end
@flush_times += 1