Module: GitLab::SecretDetection::Utils::StrongMemoize

Included in:
GRPC::Client, X509::Certificate
Defined in:
lib/gitlab/secret_detection/utils/memoize.rb

Overview

Defined Under Namespace

Modules: StrongMemoizeClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



90
91
92
# File 'lib/gitlab/secret_detection/utils/memoize.rb', line 90

def self.included(base)
  base.singleton_class.prepend(StrongMemoizeClassMethods)
end

.normalize_key(key) ⇒ Object



111
112
113
114
115
116
# File 'lib/gitlab/secret_detection/utils/memoize.rb', line 111

def normalize_key(key)
  return key unless key.end_with?('!', '?')

  # Replace invalid chars like `!` and `?` with allowed Unicode codeparts.
  key.to_s.tr('!?', "\uFF01\uFF1F")
end

Instance Method Details

#clear_memoization(name) ⇒ Object



77
78
79
80
# File 'lib/gitlab/secret_detection/utils/memoize.rb', line 77

def clear_memoization(name)
  key = ivar(StrongMemoize.normalize_key(name))
  remove_instance_variable(key) if instance_variable_defined?(key)
end

#strong_memoize(name) ⇒ Object

Instead of writing patterns like this:

def trigger_from_token
  return @trigger if defined?(@trigger)

  @trigger = Ci::Trigger.find_by_token(params[:token].to_s)
end

We could write it like:

include GitLab::SecretDetection::Utils::StrongMemoize

def trigger_from_token
  Ci::Trigger.find_by_token(params[:token].to_s)
end
strong_memoize_attr :trigger_from_token

def enabled?
  Feature.enabled?(:some_feature)
end
strong_memoize_attr :enabled?


31
32
33
34
35
36
37
38
39
# File 'lib/gitlab/secret_detection/utils/memoize.rb', line 31

def strong_memoize(name)
  key = ivar(name)

  if instance_variable_defined?(key)
    instance_variable_get(key)
  else
    instance_variable_set(key, yield)
  end
end

#strong_memoize_with(name, *args) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/gitlab/secret_detection/utils/memoize.rb', line 62

def strong_memoize_with(name, *args)
  container = strong_memoize(name) { {} }

  if container.key?(args)
    container[args]
  else
    container[args] = yield
  end
end

#strong_memoize_with_expiration(name, expire_in) ⇒ Object

Works the same way as “strong_memoize” but takes a second argument - expire_in. This allows invalidate the data after specified number of seconds



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/gitlab/secret_detection/utils/memoize.rb', line 44

def strong_memoize_with_expiration(name, expire_in)
  key = ivar(name)
  expiration_key = "#{key}_expired_at"

  if instance_variable_defined?(expiration_key)
    expire_at = instance_variable_get(expiration_key)
    clear_memoization(name) if expire_at.past?
  end

  if instance_variable_defined?(key)
    instance_variable_get(key)
  else
    value = instance_variable_set(key, yield)
    instance_variable_set(expiration_key, Time.current + expire_in)
    value
  end
end

#strong_memoized?(name) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
# File 'lib/gitlab/secret_detection/utils/memoize.rb', line 72

def strong_memoized?(name)
  key = ivar(StrongMemoize.normalize_key(name))
  instance_variable_defined?(key)
end