Class: RProxy::UsageManager

Inherits:
Object
  • Object
show all
Defined in:
lib/r_proxy/usage_manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(config, cache_pool, redis) ⇒ UsageManager

Returns a new instance of UsageManager.



3
4
5
6
7
8
9
# File 'lib/r_proxy/usage_manager.rb', line 3

def initialize(config, cache_pool, redis)
  @enable_cache = config.enable_cache
  @cache_pool = cache_pool
  @redis = redis
  @no_cache_below = config.no_cache_below
  @check_snapshot_service = RProxy::CheckSnapshotService.new(@redis, config)
end

Instance Method Details

#auth_user(user, pass) ⇒ Object



11
12
13
14
15
16
# File 'lib/r_proxy/usage_manager.rb', line 11

def auth_user(user, pass)
  value = fetch_usage(user, pass)

  return value if !value.nil? && value.to_i > 0
  nil
end

#report_usage(user, pass, value) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/r_proxy/usage_manager.rb', line 18

def report_usage(user, pass, value)
  return if user.nil? || pass.nil? || value.nil?

  key = proxy_key(user, pass)
  cache = @cache_pool[key]

  if cache.nil? || !@cache_pool.writable?
    @redis.decrby(key, value)
  else
    tmp = cache[:used]
    @cache_pool[key][:used] = tmp + value
  end
end