Skip to content

Commit 6426b43

Browse files
committed
Simplify server caching: remove redundant search index cache
The search index was double-cached in both @search_index_cache and @page_cache. Remove the dedicated cache and inline the one-liner into generate_page, letting @page_cache handle all caching. Also cache @template_dir in initialize instead of recomputing File.expand_path on every asset request.
1 parent 5c0cafd commit 6426b43

1 file changed

Lines changed: 4 additions & 21 deletions

File tree

lib/rdoc/server.rb

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ def initialize(rdoc, port)
6868
@port = port
6969

7070
@generator = create_generator
71+
@template_dir = File.expand_path(@generator.template_dir)
7172
@page_cache = {}
72-
@search_index_cache = nil
7373
@last_change_time = Time.now.to_f
7474
@mutex = Mutex.new
7575
@running = false
@@ -199,9 +199,8 @@ def serve_asset(path)
199199
rel_path = path.delete_prefix("/")
200200
asset_path = File.join(@generator.template_dir, rel_path)
201201
real_asset = File.expand_path(asset_path)
202-
real_template = File.expand_path(@generator.template_dir)
203202

204-
unless real_asset.start_with?("#{real_template}/") && File.file?(real_asset)
203+
unless real_asset.start_with?("#{@template_dir}/") && File.file?(real_asset)
205204
return [404, 'text/plain', "Asset not found: #{rel_path}"]
206205
end
207206

@@ -258,7 +257,7 @@ def generate_page(name)
258257
when 'table_of_contents.html'
259258
@generator.generate_table_of_contents
260259
when 'js/search_data.js'
261-
build_search_index
260+
"var search_data = #{JSON.generate(index: @generator.build_search_index)};"
262261
else
263262
text_name = name.chomp('.html')
264263
class_name = text_name.gsub('/', '::')
@@ -271,29 +270,13 @@ def generate_page(name)
271270
end
272271
end
273272

274-
##
275-
# Builds the search index JavaScript.
276-
277-
def build_search_index
278-
@search_index_cache ||=
279-
"var search_data = #{JSON.generate(index: @generator.build_search_index)};"
280-
end
281-
282273
##
283274
# Injects the live-reload polling script before +</body>+.
284275

285276
def inject_live_reload(html, last_change_time)
286277
html.sub('</body>', "#{self.class.live_reload_script(last_change_time)}</body>")
287278
end
288279

289-
##
290-
# Clears all cached HTML pages and the search index.
291-
292-
def invalidate_all_caches
293-
@page_cache.clear
294-
@search_index_cache = nil
295-
end
296-
297280
##
298281
# Starts a background thread that polls source file mtimes and triggers
299282
# re-parsing when changes are detected.
@@ -385,7 +368,7 @@ def reparse_and_refresh(changed_files, removed_files)
385368
@store.complete(@options.visibility)
386369

387370
@generator.refresh_store_data
388-
invalidate_all_caches
371+
@page_cache.clear
389372
@last_change_time = Time.now.to_f
390373
end
391374
end

0 commit comments

Comments
 (0)