Commit 4eeb6114 authored by Michael Kozono's avatar Michael Kozono

Merge branch 'cache-root-storage-size-limit' into 'master'

Add cache to storage_size DB hits

See merge request gitlab-org/gitlab!37303
parents e5c7075c 21b48715
...@@ -2,6 +2,10 @@ ...@@ -2,6 +2,10 @@
module EE module EE
class Namespace::RootStorageSize class Namespace::RootStorageSize
CURRENT_SIZE_CACHE_KEY = 'root_storage_current_size'
LIMIT_CACHE_KEY = 'root_storage_size_limit'
EXPIRATION_TIME = 10.minutes
def initialize(root_namespace) def initialize(root_namespace)
@root_namespace = root_namespace @root_namespace = root_namespace
end end
...@@ -17,12 +21,16 @@ module EE ...@@ -17,12 +21,16 @@ module EE
end end
def current_size def current_size
@current_size ||= root_namespace.root_storage_statistics&.storage_size @current_size ||= Rails.cache.fetch(['namespaces', root_namespace.id, CURRENT_SIZE_CACHE_KEY], expires_in: EXPIRATION_TIME) do
root_namespace.root_storage_statistics&.storage_size
end
end end
def limit def limit
@limit ||= root_namespace.actual_limits.storage_size_limit.megabytes + @limit ||= Rails.cache.fetch(['namespaces', root_namespace.id, LIMIT_CACHE_KEY], expires_in: EXPIRATION_TIME) do
root_namespace.additional_purchased_storage_size.megabytes root_namespace.actual_limits.storage_size_limit.megabytes +
root_namespace.additional_purchased_storage_size.megabytes
end
end end
private private
......
...@@ -62,6 +62,16 @@ RSpec.describe Namespace::RootStorageSize, type: :model do ...@@ -62,6 +62,16 @@ RSpec.describe Namespace::RootStorageSize, type: :model do
subject { model.current_size } subject { model.current_size }
it { is_expected.to eq(current_size) } it { is_expected.to eq(current_size) }
context 'caches values', :use_clean_rails_memory_store_caching do
let(:key) { 'root_storage_current_size' }
it 'caches the value' do
subject
expect(Rails.cache.read(['namespaces', namespace.id, key])).to eq(current_size)
end
end
end end
describe '#limit' do describe '#limit' do
...@@ -93,5 +103,20 @@ RSpec.describe Namespace::RootStorageSize, type: :model do ...@@ -93,5 +103,20 @@ RSpec.describe Namespace::RootStorageSize, type: :model do
it { is_expected.to eq(0) } it { is_expected.to eq(0) }
end end
context 'caches values', :use_clean_rails_memory_store_caching do
let(:key) { 'root_storage_size_limit' }
before do
plan_limits.update!(storage_size_limit: 70_000)
namespace.update!(additional_purchased_storage_size: 34_000)
end
it 'caches the value' do
subject
expect(Rails.cache.read(['namespaces', namespace.id, key])).to eq(104_000.megabytes)
end
end
end end
end end
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