Class: Statsig::UserPersistentStorageUtils

Inherits:
Object
  • Object
show all
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.



10
11
12
13
# File 'lib/user_persistent_storage_utils.rb', line 10

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

Instance Attribute Details

#cacheObject

Returns the value of attribute cache.



6
7
8
# File 'lib/user_persistent_storage_utils.rb', line 6

def cache
  @cache
end

#storageObject

Returns the value of attribute storage.



8
9
10
# File 'lib/user_persistent_storage_utils.rb', line 8

def storage
  @storage
end

Instance Method Details

#add_evaluation_to_user_persisted_values(user_persisted_values, config_name, evaluation) ⇒ Object



64
65
66
67
68
69
# File 'lib/user_persistent_storage_utils.rb', line 64

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



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

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



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/user_persistent_storage_utils.rb', line 22

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



56
57
58
59
60
61
62
# File 'lib/user_persistent_storage_utils.rb', line 56

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



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/user_persistent_storage_utils.rb', line 42

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