Class: Kameleoon::Hybrid::ManagerImpl

Inherits:
Manager
  • Object
show all
Defined in:
lib/kameleoon/hybrid/manager.rb

Overview

Implementation of Cache with auto cleaning feature

Instance Method Summary collapse

Constructor Details

#initialize(expiration_time, cleaning_interval, cache_factory, log_func) ⇒ ManagerImpl

Returns a new instance of ManagerImpl.



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/kameleoon/hybrid/manager.rb', line 24

def initialize(expiration_time, cleaning_interval, cache_factory, log_func)
  super()
  # synchronization is necessary for adding same visitor_code from different threads
  @mutex = Mutex.new
  @expiration_time = expiration_time
  @cache_factory = cache_factory
  @log = log_func
  # it's recommend to use cleaning_interval 3-4 times more than experiation_time for more performance
  # in this case on every cleaning iteration storage will be cleaned 2/3 - 3/4 of volume
  @cache = cache_factory.create(expiration_time, cleaning_interval)
  @log.call('Hybrid Manager was successfully initialized')
end

Instance Method Details

#add_variation(visitor_code, experiment_id, variation_id) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/kameleoon/hybrid/manager.rb', line 37

def add_variation(visitor_code, experiment_id, variation_id)
  @mutex.synchronize do
    visitor_cache = @cache.get(visitor_code)
    visitor_cache = @cache_factory.create(@expiration_time, 0) if visitor_cache.nil?
    visitor_cache.set(experiment_id, variation_id)
    @cache.set(visitor_code, visitor_cache)
  end
  @log.call("Hybrid Manager successfully added variation for visitor_code: #{visitor_code}, " \
    "experiment: #{experiment_id}, variation: #{variation_id}")
end

#get_engine_tracking_code(visitor_code) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/kameleoon/hybrid/manager.rb', line 48

def get_engine_tracking_code(visitor_code)
  tracking_code = TC_INIT
  visitor_cache = @cache.get(visitor_code)
  return tracking_code if visitor_cache.nil?

  visitor_cache.active_items.each_pair do |key, value|
    tracking_code += format(TC_ASSIGN_VARIATION_TRIGGER_FORMAT, key, value, key)
  end
  tracking_code
end