Class: HaveAPI::Fs::Cache
Overview
A path-based cache for the component tree.
Instance Attribute Summary collapse
-
#drops ⇒ Object
readonly
Returns the value of attribute drops.
-
#hits ⇒ Object
readonly
Returns the value of attribute hits.
-
#invalid ⇒ Object
readonly
Returns the value of attribute invalid.
-
#misses ⇒ Object
readonly
Returns the value of attribute misses.
Attributes inherited from Worker
Instance Method Summary collapse
-
#drop_below(path) ⇒ Object
Drop the component at
path
and all its descendants from the cache. -
#get(path, &block) ⇒ Object
Find component with
path
in the cache. -
#initialize(fs) ⇒ Cache
constructor
A new instance of Cache.
- #set(path, v) ⇒ Object
- #size ⇒ Object
- #start_delay ⇒ Object
- #work ⇒ Object
- #work_period ⇒ Object
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
#drops ⇒ Object (readonly)
Returns the value of attribute drops.
6 7 8 |
# File 'lib/haveapi/fs/cache.rb', line 6 def drops @drops end |
#hits ⇒ Object (readonly)
Returns the value of attribute hits.
6 7 8 |
# File 'lib/haveapi/fs/cache.rb', line 6 def hits @hits end |
#invalid ⇒ Object (readonly)
Returns the value of attribute invalid.
6 7 8 |
# File 'lib/haveapi/fs/cache.rb', line 6 def invalid @invalid end |
#misses ⇒ Object (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.
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
.
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 |
#size ⇒ Object
17 18 19 |
# File 'lib/haveapi/fs/cache.rb', line 17 def size @cache.size end |
#start_delay ⇒ Object
59 60 61 |
# File 'lib/haveapi/fs/cache.rb', line 59 def start_delay Cleaner::ATIME + 60 end |
#work ⇒ Object
67 68 69 |
# File 'lib/haveapi/fs/cache.rb', line 67 def work @cache.delete_if { |k, v| v.invalid? } end |