Class: Bandit::MemCacheStorage

Inherits:
BaseStorage show all
Defined in:
lib/bandit/storage/memcache.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) ⇒ MemCacheStorage

Returns a new instance of MemCacheStorage.



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

def initialize(config)
  require 'memcache'
  config[:namespace] ||= 'bandit'
  @memcache = MemCache.new(config.fetch(:host, 'localhost:11211'), config)
end

Instance Method Details

#clear!Object



38
39
40
41
42
# File 'lib/bandit/storage/memcache.rb', line 38

def clear!
  with_failure_grace(nil) {
    @memcache.flush_all
  }
end

#get(key, default = 0) ⇒ Object

get key if exists, otherwise 0



25
26
27
28
29
# File 'lib/bandit/storage/memcache.rb', line 25

def get(key, default=0)
  with_failure_grace(default) {
    @memcache.get(key) || default
  }
end

#incr(key, count = 1) ⇒ Object

increment key by count



10
11
12
13
14
15
# File 'lib/bandit/storage/memcache.rb', line 10

def incr(key, count=1)
  # memcache incr seems to be broken in memcache-client gem
  with_failure_grace(count) {
    set(key, get(key, 0) + count)
  }
end

#init(key, value) ⇒ Object

initialize key if not set



18
19
20
21
22
# File 'lib/bandit/storage/memcache.rb', line 18

def init(key, value)    
  with_failure_grace(value) {
    @memcache.add(key, value)
  }
end

#set(key, value) ⇒ Object

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



32
33
34
35
36
# File 'lib/bandit/storage/memcache.rb', line 32

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