Commit 21b48715 authored by Ragnar Hardarson's avatar Ragnar Hardarson Committed by Michael Kozono

Add cache to storage_size DB hits

parent c639cb5b
......@@ -2,6 +2,10 @@
module EE
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)
@root_namespace = root_namespace
end
......@@ -17,13 +21,17 @@ module EE
end
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
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.actual_limits.storage_size_limit.megabytes +
root_namespace.additional_purchased_storage_size.megabytes
end
end
private
......
......@@ -62,6 +62,16 @@ RSpec.describe Namespace::RootStorageSize, type: :model do
subject { model.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
describe '#limit' do
......@@ -93,5 +103,20 @@ RSpec.describe Namespace::RootStorageSize, type: :model do
it { is_expected.to eq(0) }
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
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