class PEROBS::PersistentObjectCacheLine

Constants

WATERMARK

This defines the minimum size of the cache line. If it is too large, the time to find an entry will grow too much. If it is too small the number of cache lines will be too large and create more store overhead. By running benchmarks it turned out that 8 is a pretty good compromise.

Public Class Methods

new() click to toggle source
# File lib/perobs/PersistentObjectCacheLine.rb, line 43
def initialize
  @entries = []
end

Public Instance Methods

delete(uid) click to toggle source

Delete the entry that matches the given UID @param uid [Integer]

# File lib/perobs/PersistentObjectCacheLine.rb, line 77
def delete(uid)
  @entries.delete_if { |e| e.obj.uid == uid }
end
flush(now) click to toggle source

Save all modified entries and delete all but the most recently added.

# File lib/perobs/PersistentObjectCacheLine.rb, line 82
def flush(now)
  if now || @entries.length > WATERMARK
    @entries.each do |e|
      if e.modified
        e.obj.save
        e.modified = false
      end
    end

    # Delete all but the first WATERMARK entry.
    @entries = @entries[0..WATERMARK - 1] if @entries.length > WATERMARK
  end
end
get(uid) click to toggle source
# File lib/perobs/PersistentObjectCacheLine.rb, line 63
def get(uid)
  if (index = @entries.find_index{ |e| e.obj.uid == uid })
    if index > 0
      # Move the entry to the front.
      @entries.unshift(@entries.delete_at(index))
    end
    @entries.first
  else
    nil
  end
end
insert(object, modified) click to toggle source
# File lib/perobs/PersistentObjectCacheLine.rb, line 47
def insert(object, modified)
  if (index = @entries.find_index{ |e| e.obj.uid == object.uid })
    # We have found and removed an existing entry for this particular
    # object. If the modified flag is set, ensure that the entry has it
    # set as well.
    entry = @entries.delete_at(index)
    entry.modified = true if modified && !entry.modified
  else
    # There is no existing entry for this object. Create a new one.
    entry = Entry.new(object, modified)
  end

  # Insert the entry at the beginning of the line.
  @entries.unshift(entry)
end