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:
+12
-2
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user