class HaveAPI::Fs::Cache

A path-based cache for the component tree.

Attributes

drops[R]
hits[R]
invalid[R]
misses[R]

Public Class Methods

new(fs) click to toggle source
Calls superclass method
# File lib/haveapi/fs/cache.rb, line 8
def initialize(fs)
  super
  @cache = {}
  @hits = 0
  @misses = 0
  @invalid = 0
  @drops = 0
end

Public Instance Methods

drop_below(path) click to toggle source

Drop the component at `path` and all its descendants from the cache. @param [String] path

# File lib/haveapi/fs/cache.rb, line 52
def drop_below(path)
  abs_path = '/' + path
  keys = @cache.keys.select { |k| k.start_with?(abs_path) }
  @drops += keys.count
  keys.each { |k| @cache.delete(k) }
end
get(path, &block) click to toggle source

Find component with `path` in the cache. If the component is not in the cache yet or is in an invalid state, `block` is called and its return value is saved in the cache for this `path`.

@param [String] path @yieldreturn [HaveAPI::Fs::Component]

# File lib/haveapi/fs/cache.rb, line 27
def get(path, &block)
  obj = @cache[path]

  if obj
    if obj.invalid?
      @invalid += 1
      @cache[path] = block.call

    else
      @hits += 1
      obj
    end

  else
    @misses += 1
    @cache[path] = block.call
  end
end
set(path, v) click to toggle source
# File lib/haveapi/fs/cache.rb, line 46
def set(path, v)
  @cache[path] = v
end
size() click to toggle source
# File lib/haveapi/fs/cache.rb, line 17
def size
  @cache.size
end
start_delay() click to toggle source
# File lib/haveapi/fs/cache.rb, line 59
def start_delay
  Cleaner::ATIME + 60
end
work() click to toggle source
# File lib/haveapi/fs/cache.rb, line 67
def work
  @cache.delete_if { |k, v| v.invalid? }
end
work_period() click to toggle source
# File lib/haveapi/fs/cache.rb, line 63
def work_period
  Cleaner::ATIME / 2 + 60
end