Class: Autoscaler::CounterCacheRedis

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

Overview

Implements a cache for the number of heroku works currently up This permits some web/worker communication, which makes longer timeouts practical.

Defined Under Namespace

Classes: Expired

Instance Method Summary collapse

Constructor Details

#initialize(redis, timeout = 5 * 60) ⇒ CounterCacheRedis

Returns a new instance of CounterCacheRedis.

Parameters:

  • redis (Proc, ConnectionPool, Redis client)

    redis interface Proc: e.g. Sidekiq.method(:redis) ConnectionPool: e.g. what you pass to Sidekiq.redis= Redis client: e.g. Redis.connect

  • timeout (Numeric) (defaults to: 5 * 60)

    number of seconds to allow before expiration



10
11
12
13
# File 'lib/autoscaler/counter_cache_redis.rb', line 10

def initialize(redis, timeout = 5 * 60)
  @redis = redis
  @timeout = timeout
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:



24
25
26
27
28
29
# File 'lib/autoscaler/counter_cache_redis.rb', line 24

def counter
  value = redis {|c| c.get('autoscaler:workers')}
  return value.to_i if value
  return yield if block_given?
  raise Expired
end

#counter=(value) ⇒ Object

Parameters:

  • value (Numeric)

    new counter value



16
17
18
# File 'lib/autoscaler/counter_cache_redis.rb', line 16

def counter=(value)
  redis {|c| c.setex('autoscaler:workers', @timeout, value)}
end