perf: memoize Store#all with mtime-based invalidation

Cache the parsed post list per Store instance, invalidated on save,
delete, or content_dir mtime change. Eliminates re-parsing every .md
file on every API and admin request.
This commit is contained in:
2026-06-25 22:06:49 +02:00
parent d7f9813484
commit ad00872895
+11 -1
View File
@@ -16,11 +16,17 @@ module Volumen
def initialize(content_dir, default_lang: nil)
@content_dir = File.expand_path(content_dir.to_s)
@default_lang = default_lang
@all = nil
end
def all
mtime = Dir.exist?(@content_dir) ? File.mtime(@content_dir) : nil
@all = nil if mtime != @all_mtime
@all ||= begin
@all_mtime = mtime
markdown_files.filter_map { |path| load_post(path) }
end
end
def find(slug, lang: nil)
all.find do |post|
@@ -33,12 +39,16 @@ module Volumen
FileUtils.mkdir_p(File.dirname(target))
File.write(target, post.to_file)
post.path = target
@all = nil
post
end
def delete(slug, lang: nil)
post = find(slug, lang: lang)
File.delete(post.path) if post&.path
if post&.path
File.delete(post.path)
@all = nil
end
post
end