Class: Autoscaler::CounterCacheMemory

Inherits:
Object
  • Object
show all
Defined in:
lib/autoscaler/counter_cache_memory.rb

Overview

Implements a cache for the number of heroku works currently up Values are stored for short periods in the object

Defined Under Namespace

Classes: Expired

Instance Method Summary collapse

Constructor Details

#initialize(timeout = 5) ⇒ CounterCacheMemory

Returns a new instance of CounterCacheMemory.

Parameters:

  • timeout (Numeric) (defaults to: 5)

    number of seconds to allow before expiration



6
7
8
9
10
# File 'lib/autoscaler/counter_cache_memory.rb', line 6

def initialize(timeout = 5)
  @timeout = timeout
  @counter = 0
  @valid_until = Time.now - 1
end

Instance Method Details

#counterObject

Current value. Uses the Hash#fetch api - pass a block to use in place of expired values or it will raise an exception.

Raises:



22
23
24
25
26
# File 'lib/autoscaler/counter_cache_memory.rb', line 22

def counter
  return @counter if valid?
  return yield if block_given?
  raise Expired
end

#counter=(value) ⇒ Object

Parameters:

  • value (Numeric)

    new counter value



13
14
15
16
# File 'lib/autoscaler/counter_cache_memory.rb', line 13

def counter=(value)
  @valid_until = Time.now + @timeout
  @counter = value
end