From ad00872895f3c3bfd6f25aba7df996d8c368a6de Mon Sep 17 00:00:00 2001 From: Petr Date: Thu, 25 Jun 2026 21:48:57 +0200 Subject: [PATCH] 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. --- lib/volumen/store.rb | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/volumen/store.rb b/lib/volumen/store.rb index ef74b27..4101302 100644 --- a/lib/volumen/store.rb +++ b/lib/volumen/store.rb @@ -16,10 +16,16 @@ 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 - markdown_files.filter_map { |path| load_post(path) } + 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) @@ -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