| Class | TaskJuggler::OnShiftCache |
| In: |
lib/WorkingHours.rb
|
| Parent: | Object |
This cache class is used to speedup accesses to the frequently used WorkingHours::onShift? function. It saves the result the first time the function is called for a particular date and working hour set and returns it on subsequent calls again. Each partucular set of working hours needs its separate cache. The OnShiftCache object is shared amongst all WorkingHours objects so that WorkingHours objects with identical working hours can share the cache.
Create the OnShiftCache object. There should be only one for the application.
# File lib/WorkingHours.rb, line 28
28: def initialize
29: @caches = []
30: @workingHoursTable = []
31: # The cache is an array with entries for each date. To minimize the
32: # necessary storage space, we need to guess the smallest used date
33: # (which gets index 0 then) and the smallest distance between dates.
34: @minDate = nil
35: # We assume a timing resolution of 1 hour (the TaskJuggler default)
36: # first.
37: @minDateDelta = 60 * 60
38: end
Get the value for a given cache and date.
# File lib/WorkingHours.rb, line 66
66: def get(cache, date)
67: cache[dateToIndex(date)]
68: end
Register the WorkingHours object with the caches. The function will return the actual cache used for this particular set of working hours. The WorkingHours object may not change its working hours after this call. The returned cache reference is used as a handle for subsequent OnShiftCache::set and OnShiftCache::get calls.
# File lib/WorkingHours.rb, line 45
45: def register(wh)
46: # Search the list of already registered caches for an identical set of
47: # WorkingHours. In case one is found, return the reference to this
48: # cache.
49: @workingHoursTable.length.times do |i|
50: if @workingHoursTable[i] == wh
51: return @caches[i]
52: end
53: end
54: # If this is a new set of WorkingHours we create a new cache for it.
55: @workingHoursTable << WorkingHours.new(wh)
56: @caches << []
57: @caches.last
58: end