In Files

Parent

Included Modules

Class Index [+]

Quicksearch

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/DataCache.rb, line 54
54:     def initialize
55:       resize
56:       flush
57:       # Counter for the number of writes to the cache.
58:       @stores = 0
59:       # Counter for the number of found values.
60:       @hits = 0
61:       # Counter for the number of not found values.
62:       @misses = 0
63:     end

Public Instance Methods

cached(key) click to toggle source

If we have a value for the key, return the value. Otherwise call the block to compute the value, store it and return it.

     # File lib/DataCache.rb, line 116
116:     def cached(key)
117:       if @entries.has_key?(key)
118:         e = @entries[key]
119:         @hits += 1
120:         e.value
121:       else
122:         @misses += 1
123:         store(yield, key)
124:       end
125:     end
flush() click to toggle source

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

    # File lib/DataCache.rb, line 76
76:     def flush
77:       @entries = {}
78:     end
load(key) click to toggle source

Retrieve the value indexed by key from the cache. If it’s not found, nil is returned.

     # File lib/DataCache.rb, line 104
104:     def load(key)
105:       if (e = @entries[key])
106:         @hits += 1
107:         e.value
108:       else
109:         @misses += 1
110:         nil
111:       end
112:     end
resize(size = 100000) click to toggle source

For now, we use this randomly determined size.

    # File lib/DataCache.rb, line 66
66:     def resize(size = 100000)
67:       @highWaterMark = size
68:       # Flushing out the least used entries is fairly expensive. So we only
69:       # want to do this once in a while. The lowWaterMark determines how much
70:       # of the entries will survive the flush.
71:       @lowWaterMark = size * 0.9
72:     end
store(value, key) click to toggle source

Store value into the cache using key to tag it. key must be unique and must be used to load the value from the cache again. You cannot store nil values!

     # File lib/DataCache.rb, line 83
 83:     def store(value, key)
 84:       @stores += 1
 85: 
 86:       if @entries.size > @highWaterMark
 87:         while @entries.size > @lowWaterMark
 88:           # How many entries do we need to delete to get to the low watermark?
 89:           toDelete = @entries.size - @lowWaterMark
 90:           @entries.delete_if do |foo, e|
 91:             # Hit counts age with every cleanup.
 92:             (e.hits -= 1) < 0 && (toDelete -= 1) >= 0
 93:           end
 94:         end
 95:       end
 96: 
 97:       @entries[key] = DataCacheEntry.new(value)
 98: 
 99:       value
100:     end
to_s() click to toggle source
     # File lib/DataCache.rb, line 127
127:     def to_s
128:       Entries: #{@entries.size}   Stores: #{@stores}Hits: #{@hits}   Misses: #{@misses}Hit Rate: #{@hits * 100.0 / (@hits + @misses)}%
129:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.