class TaskJuggler::DataCache

This class provides a global data cache that can be used to store and retrieve values indexed by a key. The cache is size limited. When maximum capacity is reached, a certain percentage of the least requested values is dropped from the cache. The primary purpose of this global cache is to store values that are expensive to compute but may be need on several occasions during the program execution.

Public Class Methods

new() click to toggle source
# File lib/taskjuggler/DataCache.rb, line 56
def initialize
  resize
  flush
  # Counter for the number of writes to the cache.
  @stores = 0
  # Counter for the number of found values.
  @hits = 0
  # Counter for the number of not found values.
  @misses = 0
  # Counter for hash collisions
  @collisions = 0
end

Public Instance Methods

cached(*args) { || ... } click to toggle source

Ruby 1.8 has a buggy hash key generation algorithm that leads to many hash collisions. We completely disable caching on 1.8.

# File lib/taskjuggler/DataCache.rb, line 89
def cached(*args)
  yield
end
flush() click to toggle source

Completely flush the cache. The statistic counters will remain intact, but all data values are lost.

# File lib/taskjuggler/DataCache.rb, line 80
def flush
  @entries = {}
end
resize(size = 100000) click to toggle source

For now, we use this randomly determined size.

# File lib/taskjuggler/DataCache.rb, line 70
def resize(size = 100000)
  @highWaterMark = size
  # Flushing out the least used entries is fairly expensive. So we only
  # want to do this once in a while. The lowWaterMark determines how much
  # of the entries will survive the flush.
  @lowWaterMark = size * 0.9
end
to_s() click to toggle source
# File lib/taskjuggler/DataCache.rb, line 120
    def to_s
      <<"EOT"
Entries: #{@entries.size}   Stores: #{@stores}   Collisions: #{@collisions}
Hits: #{@hits}   Misses: #{@misses}
Hit Rate: #{@hits * 100.0 / (@hits + @misses)}%
EOT
    end