Class: ThreeScaleToolbox::RemoteCache

Inherits:
BasicObject
Defined in:
lib/3scale_toolbox/remote_cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subject) ⇒ RemoteCache

Returns a new instance of RemoteCache.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/3scale_toolbox/remote_cache.rb', line 9

def initialize(subject)
  @subject = subject
  # Metrics and methods cache data
  @metrics_cache = {}
  # methods cache data
  @methods_cache = {}
  # Backend Metrics and methods cache data
  @backend_metrics_cache = {}
  # Backend methods cache data
  @backend_methods_cache = {}
  # Backends cache data
  @backends_cache = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Generic methods



184
185
186
187
# File 'lib/3scale_toolbox/remote_cache.rb', line 184

def method_missing(name, *args)
  # Correct delegation https://eregon.me/blog/2021/02/13/correct-delegation-in-ruby-2-27-3.html
  @subject.public_send(name, *args)
end

Instance Attribute Details

#backend_methods_cacheObject (readonly)

Returns the value of attribute backend_methods_cache.



4
5
6
# File 'lib/3scale_toolbox/remote_cache.rb', line 4

def backend_methods_cache
  @backend_methods_cache
end

#backend_metrics_cacheObject (readonly)

Returns the value of attribute backend_metrics_cache.



4
5
6
# File 'lib/3scale_toolbox/remote_cache.rb', line 4

def backend_metrics_cache
  @backend_metrics_cache
end

#backends_cacheObject (readonly)

Returns the value of attribute backends_cache.



4
5
6
# File 'lib/3scale_toolbox/remote_cache.rb', line 4

def backends_cache
  @backends_cache
end

#methods_cacheObject (readonly)

Returns the value of attribute methods_cache.



4
5
6
# File 'lib/3scale_toolbox/remote_cache.rb', line 4

def methods_cache
  @methods_cache
end

#metrics_cacheObject (readonly)

Returns the value of attribute metrics_cache.



4
5
6
# File 'lib/3scale_toolbox/remote_cache.rb', line 4

def metrics_cache
  @metrics_cache
end

#subjectObject (readonly)

Returns the value of attribute subject.



4
5
6
# File 'lib/3scale_toolbox/remote_cache.rb', line 4

def subject
  @subject
end

Instance Method Details

#backend(id) ⇒ Object



170
171
172
173
174
175
176
177
178
# File 'lib/3scale_toolbox/remote_cache.rb', line 170

def backend(id)
  # if exist in the cache, return it.
  # But if not, it is not populated because cache is paginated and page is unknown.
  if (backend_attrs = backends_cache.values.reduce([], :concat).find { |attrs| attrs['id'] == id })
    return backend_attrs
  end

  subject.backend(id)
end

#create_backend(attributes) ⇒ Object



152
153
154
155
156
# File 'lib/3scale_toolbox/remote_cache.rb', line 152

def create_backend(attributes)
  subject.create_backend(attributes).tap do |backend_attrs|
    backends_cache.clear unless backend_attrs.respond_to?(:has_key?) && !backend_attrs['errors'].nil?
  end
end

#create_backend_method(backend_id, metric_id, attributes) ⇒ Object



119
120
121
122
123
124
125
126
# File 'lib/3scale_toolbox/remote_cache.rb', line 119

def create_backend_method(backend_id, metric_id, attributes)
  subject.create_backend_method(backend_id, metric_id, attributes).tap do |method_attrs|
    unless method_attrs.respond_to?(:has_key?) && !method_attrs['errors'].nil?
      backend_metrics_cache.delete(backend_id)
      backend_methods_cache.delete(method_cache_key(backend_id, metric_id))
    end
  end
end

#create_backend_metric(backend_id, attributes) ⇒ Object



101
102
103
104
105
# File 'lib/3scale_toolbox/remote_cache.rb', line 101

def create_backend_metric(backend_id, attributes)
  subject.create_backend_metric(backend_id, attributes).tap do |metric_attrs|
    backend_metrics_cache.delete(backend_id) unless metric_attrs.respond_to?(:has_key?) && !metric_attrs['errors'].nil?
  end
end

#create_method(service_id, metric_id, attributes) ⇒ Object



66
67
68
69
70
71
# File 'lib/3scale_toolbox/remote_cache.rb', line 66

def create_method(service_id, metric_id, attributes)
  subject.create_method(service_id, metric_id, attributes).tap do |method_attrs|
    metrics_cache.delete(service_id) unless method_attrs.respond_to?(:has_key?) && !method_attrs['errors'].nil?
    methods_cache.delete(method_cache_key(service_id, metric_id)) unless method_attrs.respond_to?(:has_key?) && !method_attrs['errors'].nil?
  end
end

#create_metric(service_id, attributes) ⇒ Object



40
41
42
43
44
# File 'lib/3scale_toolbox/remote_cache.rb', line 40

def create_metric(service_id, attributes)
  subject.create_metric(service_id, attributes).tap do |metric_attrs|
    metrics_cache.delete(service_id) unless metric_attrs.respond_to?(:has_key?) && !metric_attrs['errors'].nil?
  end
end

#delete_backend(id) ⇒ Object



164
165
166
167
168
# File 'lib/3scale_toolbox/remote_cache.rb', line 164

def delete_backend(id)
  subject.delete_backend(id).tap do |resp| 
    backends_cache.clear unless resp.respond_to?(:has_key?) && !resp['errors'].nil?
  end
end

#delete_backend_method(backend_id, metric_id, method_id) ⇒ Object



128
129
130
131
132
133
# File 'lib/3scale_toolbox/remote_cache.rb', line 128

def delete_backend_method(backend_id, metric_id, method_id)
  subject.delete_backend_method(backend_id, metric_id, method_id).tap do |_|
    backend_metrics_cache.delete(backend_id)
    backend_methods_cache.delete(method_cache_key(backend_id, metric_id))
  end
end

#delete_backend_metric(backend_id, metric_id) ⇒ Object



113
114
115
116
117
# File 'lib/3scale_toolbox/remote_cache.rb', line 113

def delete_backend_metric(backend_id, metric_id)
  subject.delete_backend_metric(backend_id, metric_id).tap do |_|
    backend_metrics_cache.delete(backend_id)
  end
end

#delete_method(service_id, parent_id, id) ⇒ Object



73
74
75
76
77
78
# File 'lib/3scale_toolbox/remote_cache.rb', line 73

def delete_method(service_id, parent_id, id)
  subject.delete_method(service_id, parent_id, id).tap do |_|
    metrics_cache.delete(service_id)
    methods_cache.delete(method_cache_key(service_id, parent_id))
  end
end

#delete_metric(service_id, metric_id) ⇒ Object



52
53
54
55
56
# File 'lib/3scale_toolbox/remote_cache.rb', line 52

def delete_metric(service_id, metric_id)
  subject.delete_metric(service_id, metric_id).tap do |_|
    metrics_cache.delete(service_id)
  end
end

#list_backend_methods(backend_id, metric_id) ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/3scale_toolbox/remote_cache.rb', line 92

def list_backend_methods(backend_id, metric_id)
  key = method_cache_key(backend_id, metric_id)
  return backend_methods_cache[key] if backend_methods_cache.has_key? key

  subject.list_backend_methods(backend_id, metric_id).tap do |methods|
    backend_methods_cache[key] = methods unless methods.respond_to?(:has_key?) && !methods['errors'].nil?
  end
end

#list_backend_metrics(backend_id) ⇒ Object

Backends



84
85
86
87
88
89
90
# File 'lib/3scale_toolbox/remote_cache.rb', line 84

def list_backend_metrics(backend_id)
  return backend_metrics_cache[backend_id] if backend_metrics_cache.has_key? backend_id

  subject.list_backend_metrics(backend_id).tap do |metrics|
    backend_metrics_cache[backend_id] = metrics unless metrics.respond_to?(:has_key?) && !metrics['errors'].nil?
  end
end

#list_backends(params = nil) ⇒ Object



144
145
146
147
148
149
150
# File 'lib/3scale_toolbox/remote_cache.rb', line 144

def list_backends(params = nil)
  return backends_cache[params] if backends_cache.has_key? params

  subject.list_backends(params).tap do |backends|
    backends_cache[params] = backends unless backends.respond_to?(:has_key?) && !backends['errors'].nil?
  end
end

#list_methods(service_id, metric_id) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/3scale_toolbox/remote_cache.rb', line 31

def list_methods(service_id, metric_id)
  key = method_cache_key(service_id, metric_id)
  return methods_cache[key] if methods_cache.has_key? key

  subject.list_methods(service_id, metric_id).tap do |methods|
    methods_cache[key] = methods unless methods.respond_to?(:has_key?) && !methods['errors'].nil?
  end
end

#list_metrics(service_id) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/3scale_toolbox/remote_cache.rb', line 23

def list_metrics(service_id)
  return metrics_cache[service_id] if metrics_cache.has_key? service_id

  subject.list_metrics(service_id).tap do |metrics|
    metrics_cache[service_id] = metrics unless metrics.respond_to?(:has_key?) && !metrics['errors'].nil?
  end
end

#public_send(name, *args) ⇒ Object



190
191
192
# File 'lib/3scale_toolbox/remote_cache.rb', line 190

def public_send(name, *args)
  method_missing(name, *args)
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


194
195
196
# File 'lib/3scale_toolbox/remote_cache.rb', line 194

def respond_to_missing?(method_name, include_private = false)
  super
end

#update_backend(id, attributes) ⇒ Object



158
159
160
161
162
# File 'lib/3scale_toolbox/remote_cache.rb', line 158

def update_backend(id, attributes)
  subject.update_backend(id, attributes).tap do |backend_attrs|
    backends_cache.clear unless backend_attrs.respond_to?(:has_key?) && !backend_attrs['errors'].nil?
  end
end

#update_backend_method(backend_id, metric_id, method_id, attributes) ⇒ Object



135
136
137
138
139
140
141
142
# File 'lib/3scale_toolbox/remote_cache.rb', line 135

def update_backend_method(backend_id, metric_id, method_id, attributes)
  subject.update_backend_method(backend_id, metric_id, method_id, attributes).tap do |method_attrs|
    unless method_attrs.respond_to?(:has_key?) && !method_attrs['errors'].nil?
      backend_metrics_cache.delete(backend_id)
      backend_methods_cache.delete(method_cache_key(backend_id, metric_id))
    end
  end
end

#update_backend_metric(backend_id, metric_id, attributes) ⇒ Object



107
108
109
110
111
# File 'lib/3scale_toolbox/remote_cache.rb', line 107

def update_backend_metric(backend_id, metric_id, attributes)
  subject.update_backend_metric(backend_id, metric_id, attributes).tap do |metric_attrs|
    backend_metrics_cache.delete(backend_id) unless metric_attrs.respond_to?(:has_key?) && !metric_attrs['errors'].nil?
  end
end

#update_method(service_id, parent_id, id, attributes) ⇒ Object



58
59
60
61
62
63
# File 'lib/3scale_toolbox/remote_cache.rb', line 58

def update_method(service_id, parent_id, id, attributes)
  subject.update_method(service_id, parent_id, id, attributes).tap do |method_attrs|
    metrics_cache.delete(service_id) unless method_attrs.respond_to?(:has_key?) && !method_attrs['errors'].nil?
    methods_cache.delete(method_cache_key(service_id, parent_id)) unless method_attrs.respond_to?(:has_key?) && !method_attrs['errors'].nil?
  end
end

#update_metric(service_id, metric_id, attributes) ⇒ Object



46
47
48
49
50
# File 'lib/3scale_toolbox/remote_cache.rb', line 46

def update_metric(service_id, metric_id, attributes)
  subject.update_metric(service_id, metric_id, attributes).tap do |metric_attrs|
    metrics_cache.delete(service_id) unless metric_attrs.respond_to?(:has_key?) && !metric_attrs['errors'].nil?
  end
end