Class: Metrics::Rails::CounterCache

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/metrics/rails/counter_cache.rb

Instance Method Summary collapse

Constructor Details

#initializeCounterCache

Returns a new instance of CounterCache.



9
10
11
12
# File 'lib/metrics/rails/counter_cache.rb', line 9

def initialize
  @cache = {}
  @lock = Mutex.new
end

Instance Method Details

#[](key) ⇒ Object



14
15
16
# File 'lib/metrics/rails/counter_cache.rb', line 14

def [](key)
  @lock.synchronize { @cache[key.to_s] }
end

#delete_allObject



18
19
20
# File 'lib/metrics/rails/counter_cache.rb', line 18

def delete_all
  @lock.synchronize { @cache.clear }
end

#flush_to(queue) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/metrics/rails/counter_cache.rb', line 22

def flush_to(queue)
  @lock.synchronize do
    @cache.each do |key, value| 
      queue.add key => {:type => :counter, :value => value}
    end
  end
end

#increment(counter, by = 1) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/metrics/rails/counter_cache.rb', line 30

def increment(counter, by=1)
  counter = counter.to_s
  @lock.synchronize do
    @cache[counter] ||= 0
    @cache[counter] += by
  end
end