Class: Bandit::RedisStorage

Inherits:
BaseStorage show all
Defined in:
lib/bandit/storage/redis.rb

Instance Method Summary collapse

Methods inherited from BaseStorage

#alt_started_key, #alternative_start_time, #conv_key, #conversion_count, get_storage, #incr_conversions, #incr_participants, #make_key, #part_key, #participant_count, #player_state_get, #player_state_key, #player_state_set, #with_failure_grace

Constructor Details

#initialize(config) ⇒ RedisStorage

Returns a new instance of RedisStorage.



3
4
5
6
7
8
9
# File 'lib/bandit/storage/redis.rb', line 3

def initialize(config)
  require 'redis'
  config[:host] ||= 'localhost'
  config[:port] ||= 6379
  config[:db] ||= "bandit"
  @redis = Redis.new config
end

Instance Method Details

#clear!Object



41
42
43
44
45
# File 'lib/bandit/storage/redis.rb', line 41

def clear!
  with_failure_grace(nil) {
    @redis.flushdb
  }
end

#get(key, default = 0) ⇒ Object

get key if exists, otherwise 0



26
27
28
29
30
31
32
# File 'lib/bandit/storage/redis.rb', line 26

def get(key, default=0)
  with_failure_grace(default) {
    val = @redis.get(key)
    return default if val.nil?
    val.numeric? ? val.to_i : val
  }
end

#incr(key, count = 1) ⇒ Object

increment key by count



12
13
14
15
16
# File 'lib/bandit/storage/redis.rb', line 12

def incr(key, count=1)
  with_failure_grace(count) {
    @redis.incrby(key, count)
  }
end

#init(key, value) ⇒ Object

initialize key if not set



19
20
21
22
23
# File 'lib/bandit/storage/redis.rb', line 19

def init(key, value)
  with_failure_grace(value) {
    @redis.set(key, value) if get(key, nil).nil?
  }
end

#set(key, value) ⇒ Object

set key with value, regardless of whether it is set or not



35
36
37
38
39
# File 'lib/bandit/storage/redis.rb', line 35

def set(key, value)
  with_failure_grace(value) {
    @redis.set(key, value)
  }
end