Class: HaveAPI::Fs::Cache

Inherits:
Worker
  • Object
show all
Defined in:
lib/haveapi/fs/cache.rb

Overview

A path-based cache for the component tree.

Instance Attribute Summary collapse

Attributes inherited from Worker

#runs

Instance Method Summary collapse

Methods inherited from Worker

#last_time, #next_time, #start, #stop

Constructor Details

#initialize(fs) ⇒ Cache

Returns a new instance of Cache.



8
9
10
11
12
13
14
15
# File 'lib/haveapi/fs/cache.rb', line 8

def initialize(fs)
  super
  @cache = {}
  @hits = 0
  @misses = 0
  @invalid = 0
  @drops = 0
end

Instance Attribute Details

#dropsObject (readonly)

Returns the value of attribute drops.



6
7
8
# File 'lib/haveapi/fs/cache.rb', line 6

def drops
  @drops
end

#hitsObject (readonly)

Returns the value of attribute hits.



6
7
8
# File 'lib/haveapi/fs/cache.rb', line 6

def hits
  @hits
end

#invalidObject (readonly)

Returns the value of attribute invalid.



6
7
8
# File 'lib/haveapi/fs/cache.rb', line 6

def invalid
  @invalid
end

#missesObject (readonly)

Returns the value of attribute misses.



6
7
8
# File 'lib/haveapi/fs/cache.rb', line 6

def misses
  @misses
end

Instance Method Details

#drop_below(path) ⇒ Object

Drop the component at path and all its descendants from the cache.

Parameters:



52
53
54
55
56
57
# 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) ⇒ Object

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.

Parameters:

Yield Returns:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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) ⇒ Object



46
47
48
# File 'lib/haveapi/fs/cache.rb', line 46

def set(path, v)
  @cache[path] = v
end

#sizeObject



17
18
19
# File 'lib/haveapi/fs/cache.rb', line 17

def size
  @cache.size
end

#start_delayObject



59
60
61
# File 'lib/haveapi/fs/cache.rb', line 59

def start_delay
  Cleaner::ATIME + 60
end

#workObject



67
68
69
# File 'lib/haveapi/fs/cache.rb', line 67

def work
  @cache.delete_if { |k, v| v.invalid? }
end

#work_periodObject



63
64
65
# File 'lib/haveapi/fs/cache.rb', line 63

def work_period
  Cleaner::ATIME / 2 + 60
end