File tree Expand file tree Collapse file tree 2 files changed +35
-3
lines changed
spec/http_server/handlers
src/invidious/http_server Expand file tree Collapse file tree 2 files changed +35
-3
lines changed Original file line number Diff line number Diff line change @@ -198,5 +198,36 @@ Spectator.describe StaticAssetsHandler do
198
198
end
199
199
end
200
200
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
+
201
232
after_each { Invidious ::HttpServer ::StaticAssetsHandler .clear_cache }
202
233
end
Original file line number Diff line number Diff line change @@ -20,6 +20,7 @@ module Invidious::HttpServer
20
20
end
21
21
22
22
CACHE_LIMIT = 5_000_000 # 5MB
23
+ @@current_cache_size = 0
23
24
@@cached_files = {} of Path => CachedFile
24
25
25
26
# Returns metadata for the requested file
@@ -71,9 +72,8 @@ module Invidious::HttpServer
71
72
72
73
# Writes file data to the cache
73
74
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)
77
77
end
78
78
end
79
79
@@ -112,6 +112,7 @@ module Invidious::HttpServer
112
112
#
113
113
# This is only used in the specs to clear the cache before each handler test
114
114
def self.clear_cache
115
+ @@current_cache_size = 0
115
116
return @@cached_files .clear
116
117
end
117
118
end
You can’t perform that action at this time.
0 commit comments