Improve cache size check to be more performant

Summing the sizes of each cached file every time is very inefficient.
Instead we can simply store the cache size in an constant and increase
it everytime a file is added into the cache.
This commit is contained in:
syeopite 2025-06-03 17:10:10 -07:00
parent 52865ff0a2
commit c012206f80
No known key found for this signature in database
GPG Key ID: A73C186DA3955A1A
2 changed files with 35 additions and 3 deletions

View File

@ -198,5 +198,36 @@ Spectator.describe StaticAssetsHandler do
end end
end end
it "Will not cache additional files if the cache limit is reached" do
5.times do |times|
data = "a" * 1_000_000
make_temporary_file("test cache size limit #{times}") do |temporary_file, file_link|
cycle_temporary_file_contents(temporary_file, data) do
response = handle HTTP::Request.new("GET", file_link)
expect(response.status_code).to eq(200)
expect(response.body).to eq(data)
end
response = handle HTTP::Request.new("GET", file_link)
expect(response.status_code).to eq(200)
expect(response.body).to eq(data)
end
end
# Cache should be 5 mb so no more files will be cached.
make_temporary_file("test cache size limit uncached") do |temporary_file, file_link|
cycle_temporary_file_contents(temporary_file, "a") do
response = handle HTTP::Request.new("GET", file_link)
expect(response.status_code).to eq(200)
expect(response.body).to eq("a")
end
response = handle HTTP::Request.new("GET", file_link)
expect(response.status_code).to eq(200)
expect(response.body).to_not eq("a")
end
end
after_each { Invidious::HttpServer::StaticAssetsHandler.clear_cache } after_each { Invidious::HttpServer::StaticAssetsHandler.clear_cache }
end end

View File

@ -20,6 +20,7 @@ module Invidious::HttpServer
end end
CACHE_LIMIT = 5_000_000 # 5MB CACHE_LIMIT = 5_000_000 # 5MB
@@current_cache_size = 0
@@cached_files = {} of Path => CachedFile @@cached_files = {} of Path => CachedFile
# Returns metadata for the requested file # Returns metadata for the requested file
@ -71,9 +72,8 @@ module Invidious::HttpServer
# Writes file data to the cache # Writes file data to the cache
private def flush_io_to_cache(io, file_path, file_info) private def flush_io_to_cache(io, file_path, file_info)
if @@cached_files.sum(&.[1].size) + file_info.size < CACHE_LIMIT if (@@current_cache_size += file_info.size) <= CACHE_LIMIT
data_slice = io.to_slice @@cached_files[file_path] = CachedFile.new(io.to_slice, file_info.size, file_info.modification_time)
@@cached_files[file_path] = CachedFile.new(data_slice, file_info.size, file_info.modification_time)
end end
end end
@ -112,6 +112,7 @@ module Invidious::HttpServer
# #
# This is only used in the specs to clear the cache before each handler test # This is only used in the specs to clear the cache before each handler test
def self.clear_cache def self.clear_cache
@@current_cache_size = 0
return @@cached_files.clear return @@cached_files.clear
end end
end end