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)
|
def initialize(content_dir, default_lang: nil)
|
||||||
@content_dir = File.expand_path(content_dir.to_s)
|
@content_dir = File.expand_path(content_dir.to_s)
|
||||||
@default_lang = default_lang
|
@default_lang = default_lang
|
||||||
|
@all = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def all
|
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
|
end
|
||||||
|
|
||||||
def find(slug, lang: nil)
|
def find(slug, lang: nil)
|
||||||
@@ -33,12 +39,16 @@ module Volumen
|
|||||||
FileUtils.mkdir_p(File.dirname(target))
|
FileUtils.mkdir_p(File.dirname(target))
|
||||||
File.write(target, post.to_file)
|
File.write(target, post.to_file)
|
||||||
post.path = target
|
post.path = target
|
||||||
|
@all = nil
|
||||||
post
|
post
|
||||||
end
|
end
|
||||||
|
|
||||||
def delete(slug, lang: nil)
|
def delete(slug, lang: nil)
|
||||||
post = find(slug, lang: lang)
|
post = find(slug, lang: lang)
|
||||||
File.delete(post.path) if post&.path
|
if post&.path
|
||||||
|
File.delete(post.path)
|
||||||
|
@all = nil
|
||||||
|
end
|
||||||
post
|
post
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user