Add system-wide file read cache
This commit is contained in:
parent
827d5fbb1d
commit
ffa662a917
14 changed files with 76 additions and 55 deletions
|
@ -3,7 +3,12 @@ require 'middleman-core/contracts'
|
|||
|
||||
module Middleman
|
||||
# The standard "record" that contains information about a file on disk.
|
||||
SourceFile = Struct.new :relative_path, :full_path, :directory, :types
|
||||
SourceFile = Struct.new(:relative_path, :full_path, :directory, :types, :version) do
|
||||
def read
|
||||
::Middleman::Sources.file_cache[full_path] ||= {}
|
||||
::Middleman::Sources.file_cache[full_path][version] ||= ::File.read(full_path)
|
||||
end
|
||||
end
|
||||
|
||||
# Sources handle multiple on-disk collections of files which make up
|
||||
# a Middleman project. They are separated by `type` which can then be
|
||||
|
@ -36,6 +41,8 @@ module Middleman
|
|||
# Reference to the global logger.
|
||||
def_delegator :@app, :logger
|
||||
|
||||
cattr_accessor :file_cache
|
||||
|
||||
# Built-in types
|
||||
# :source, :data, :locales, :reload
|
||||
|
||||
|
@ -50,6 +57,8 @@ module Middleman
|
|||
@watchers = watchers
|
||||
@sorted_watchers = @watchers.dup.freeze
|
||||
|
||||
::Middleman::Sources.file_cache = {}
|
||||
|
||||
@options = options
|
||||
|
||||
# Set of procs wanting to be notified of changes
|
||||
|
@ -177,9 +186,10 @@ module Middleman
|
|||
# @return [Middleman::SourceFile, nil]
|
||||
Contract Or[Symbol, ArrayOf[Symbol], SetOf[Symbol]], String, Maybe[Bool] => Maybe[SourceFile]
|
||||
def find(types, path, glob=false)
|
||||
array_of_types = Array(types)
|
||||
|
||||
watchers
|
||||
.lazy
|
||||
.select { |d| Array(types).include?(d.type) }
|
||||
.select { |d| array_of_types.include?(d.type) }
|
||||
.map { |d| d.find(path, glob) }
|
||||
.reject(&:nil?)
|
||||
.first
|
||||
|
@ -193,7 +203,6 @@ module Middleman
|
|||
Contract Or[Symbol, ArrayOf[Symbol], SetOf[Symbol]], String => Bool
|
||||
def exists?(types, path)
|
||||
watchers
|
||||
.lazy
|
||||
.select { |d| Array(types).include?(d.type) }
|
||||
.any? { |d| d.exists?(path) }
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue