Commit 102485c9 authored by Piotr Skorupa's avatar Piotr Skorupa

Change DatabaseMetric to cache only with cache key passed

parent 649d6cb3
......@@ -46,7 +46,7 @@ module Gitlab
end
def value
start, finish = get_or_cache_batch_ids if batch_query?
start, finish = get_or_cache_batch_ids
method(self.class.metric_operation)
.call(relation,
......@@ -69,10 +69,6 @@ module Gitlab
private
def batch_query?
self.class.metric_start.present? && self.class.metric_finish.present?
end
def relation
self.class.metric_relation.call.where(time_constraints)
end
......@@ -91,11 +87,9 @@ module Gitlab
end
def get_or_cache_batch_ids
key_name = if self.class.cache_key.present?
"metric_instrumentation/#{self.class.cache_key}"
else
"metric_instrumentation/#{relation.table_name}"
end
return [self.class.start, self.class.finish] unless self.class.cache_key.present?
key_name = "metric_instrumentation/#{self.class.cache_key}"
start = Rails.cache.fetch("#{key_name}_minimum_id", expires_in: 1.day) do
self.class.start
......
......@@ -25,11 +25,11 @@ RSpec.describe Gitlab::Usage::Metrics::Instrumentations::DatabaseMetric do
expect(subject.value).to eq(3)
end
it 'caches the result of start and finish', :use_clean_rails_redis_caching do
it 'does not cache the result of start and finish', :use_clean_rails_redis_caching do
subject.value
expect(Rails.cache.read('metric_instrumentation/issues_minimum_id')).to eq(314)
expect(Rails.cache.read('metric_instrumentation/issues_maximum_id')).to eq(949)
expect(Rails.cache.read('metric_instrumentation/issues_minimum_id')).to eq(nil)
expect(Rails.cache.read('metric_instrumentation/issues_maximum_id')).to eq(nil)
end
context 'with start and finish not called' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment