Module: RecordCache::Statistics

Defined in:
lib/record_cache/statistics.rb

Overview

Collect cache hit/miss statistics for each cache strategy

Defined Under Namespace

Classes: Counter

Class Method Summary collapse

Class Method Details

.active?Boolean

returns true if statistics need to be collected

Returns:

  • (Boolean)


9
10
11
# File 'lib/record_cache/statistics.rb', line 9

def active?
  !!@active
end

.find(base = nil, attribute = nil) ⇒ Object

Retrieve the statistics for the given base and attribute Returns a hash => <statistics for a model if no strategy is provided Returns a hash of hashes { <model_name> => => <statistics } if no parameter is provided



38
39
40
41
42
43
# File 'lib/record_cache/statistics.rb', line 38

def find(base = nil, attribute = nil)
  stats = (@stats ||= {})
  stats = (stats[base.name] ||= {}) if base
  stats = (stats[attribute] ||= Counter.new) if attribute
  stats
end

.reset!(base = nil) ⇒ Object

reset all statistics



29
30
31
32
33
# File 'lib/record_cache/statistics.rb', line 29

def reset!(base = nil)
  stats = find(base).values
  stats = stats.map(&:values).flatten unless base # flatten hash of hashes in case base was nil
  stats.each{ |s| s.reset! }
end

.startObject

start statistics collection



14
15
16
# File 'lib/record_cache/statistics.rb', line 14

def start
  @active = true
end

.stopObject

stop statistics collection



19
20
21
# File 'lib/record_cache/statistics.rb', line 19

def stop
  @active = false
end

.toggleObject

toggle statistics collection



24
25
26
# File 'lib/record_cache/statistics.rb', line 24

def toggle
  @active = !@active
end