Class: Statsig::UserPersistentStorageUtils

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/user_persistent_storage_utils.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ UserPersistentStorageUtils

Returns a new instance of UserPersistentStorageUtils.



19
20
21
22
# File 'lib/user_persistent_storage_utils.rb', line 19

def initialize(options)
  @storage = options.user_persistent_storage
  @cache = {}
end

Instance Attribute Details

#cacheObject

Returns the value of attribute cache.



13
14
15
# File 'lib/user_persistent_storage_utils.rb', line 13

def cache
  @cache
end

#storageObject

Returns the value of attribute storage.



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

def storage
  @storage
end

Instance Method Details

#add_evaluation_to_user_persisted_values(user_persisted_values, config_name, evaluation) ⇒ Object



78
79
80
81
82
83
# File 'lib/user_persistent_storage_utils.rb', line 78

def add_evaluation_to_user_persisted_values(user_persisted_values, config_name, evaluation)
  if user_persisted_values.nil?
    user_persisted_values = {}
  end
  user_persisted_values[config_name] = evaluation.to_hash
end

#get_user_persisted_values(user, id_type) ⇒ Object



25
26
27
28
29
30
# File 'lib/user_persistent_storage_utils.rb', line 25

def get_user_persisted_values(user, id_type)
  key = self.class.get_storage_key(user, id_type)
  return @cache[key] unless @cache[key].nil?

  return load_from_storage(key)
end

#load_from_storage(key) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/user_persistent_storage_utils.rb', line 33

def load_from_storage(key)
  return if @storage.nil?

  begin
    storage_values = @storage.load(key)
  rescue StandardError => e
    puts "Failed to load key (#{key}) from user_persisted_storage (#{e.message})"
    return nil
  end

  unless storage_values.nil?
    parsed_values = self.class.parse(storage_values)
    unless parsed_values.nil?
      @cache[key] = parsed_values
      return @cache[key]
    end
  end
  return nil
end

#remove_experiment_from_storage(user, id_type, config_name) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/user_persistent_storage_utils.rb', line 69

def remove_experiment_from_storage(user, id_type, config_name)
  persisted_values = get_user_persisted_values(user, id_type)
  unless persisted_values.nil?
    persisted_values.delete(config_name)
    save_to_storage(user, id_type, persisted_values)
  end
end

#save_to_storage(user, id_type, user_persisted_values) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/user_persistent_storage_utils.rb', line 54

def save_to_storage(user, id_type, user_persisted_values)
  return if @storage.nil?

  key = self.class.get_storage_key(user, id_type)
  stringified = self.class.stringify(user_persisted_values)
  return if stringified.nil?

  begin
    @storage.save(key, stringified)
  rescue StandardError => e
    puts "Failed to save key (#{key}) to user_persisted_storage (#{e.message})"
  end
end