Skip to content

Commit 15086f4

Browse files
committed
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.
1 parent d002b08 commit 15086f4

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

spec/http_server/handlers/static_assets_handler_spec.cr

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,5 +198,36 @@ Spectator.describe StaticAssetsHandler do
198198
end
199199
end
200200

201+
it "Will not cache additional files if the cache limit is reached" do
202+
5.times do |times|
203+
data = "a" * 1_000_000
204+
205+
make_temporary_file("test cache size limit #{times}") do |temporary_file, file_link|
206+
cycle_temporary_file_contents(temporary_file, data) do
207+
response = handle HTTP::Request.new("GET", file_link)
208+
expect(response.status_code).to eq(200)
209+
expect(response.body).to eq(data)
210+
end
211+
212+
response = handle HTTP::Request.new("GET", file_link)
213+
expect(response.status_code).to eq(200)
214+
expect(response.body).to eq(data)
215+
end
216+
end
217+
218+
# Cache should be 5 mb so no more files will be cached.
219+
make_temporary_file("test cache size limit uncached") do |temporary_file, file_link|
220+
cycle_temporary_file_contents(temporary_file, "a") do
221+
response = handle HTTP::Request.new("GET", file_link)
222+
expect(response.status_code).to eq(200)
223+
expect(response.body).to eq("a")
224+
end
225+
226+
response = handle HTTP::Request.new("GET", file_link)
227+
expect(response.status_code).to eq(200)
228+
expect(response.body).to_not eq("a")
229+
end
230+
end
231+
201232
after_each { Invidious::HttpServer::StaticAssetsHandler.clear_cache }
202233
end

src/invidious/http_server/static_assets_handler.cr

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module Invidious::HttpServer
2020
end
2121

2222
CACHE_LIMIT = 5_000_000 # 5MB
23+
@@current_cache_size = 0
2324
@@cached_files = {} of Path => CachedFile
2425

2526
# Returns metadata for the requested file
@@ -71,9 +72,8 @@ module Invidious::HttpServer
7172

7273
# Writes file data to the cache
7374
private def flush_io_to_cache(io, file_path, file_info)
74-
if @@cached_files.sum(&.[1].size) + file_info.size < CACHE_LIMIT
75-
data_slice = io.to_slice
76-
@@cached_files[file_path] = CachedFile.new(data_slice, file_info.size, file_info.modification_time)
75+
if (@@current_cache_size += file_info.size) <= CACHE_LIMIT
76+
@@cached_files[file_path] = CachedFile.new(io.to_slice, file_info.size, file_info.modification_time)
7777
end
7878
end
7979

@@ -112,6 +112,7 @@ module Invidious::HttpServer
112112
#
113113
# This is only used in the specs to clear the cache before each handler test
114114
def self.clear_cache
115+
@@current_cache_size = 0
115116
return @@cached_files.clear
116117
end
117118
end

0 commit comments

Comments
 (0)