Cache: change to a Singleton object so it is only loaded from disk once

This commit is contained in:
josh committed 2014-04-15 14:02:26 -04:00
1 parent 5ce8940d54
commit 160434b903
6 files changed
+54 -35

No files matched your search

+2 -2
View File
@@ -23,7 +23,7 @@ module Rscons
# Remove all generated files
def self.clean
cache = Cache.new
cache = Cache.instance
# remove all built files
cache.targets.each do |target|
FileUtils.rm_f(target)
@@ -35,7 +35,7 @@ module Rscons
Dir.rmdir(directory) rescue nil
end
end
Cache.clear
cache.clear
end
# Return whether the given path is an absolute filesystem path or not
+26 -18
View File
@@ -2,6 +2,7 @@ require "digest/md5"
require "fileutils"
require "json"
require "set"
require "singleton"
require "rscons/version"
module Rscons
@@ -50,30 +51,25 @@ module Rscons
# },
# }
class Cache
#### Constants
include Singleton
# Name of the file to store cache information in
CACHE_FILE = ".rsconscache"
#### Class Methods
# Remove the cache file
def self.clear
FileUtils.rm_f(CACHE_FILE)
end
#### Instance Methods
# Create a Cache object and load in the previous contents from the cache
# file.
def initialize
@cache = JSON.load(File.read(CACHE_FILE)) rescue {}
unless @cache.is_a?(Hash)
$stderr.puts "Warning: #{CACHE_FILE} was corrupt. Contents:\n#{@cache.inspect}"
@cache = {}
end
@cache["targets"] ||= {}
@cache["directories"] ||= {}
initialize!
end
# Remove the cache file.
def clear
FileUtils.rm_f(CACHE_FILE)
initialize!
end
# Clear the cached file checksums.
def clear_checksum_cache!
@lookup_checksums = {}
end
@@ -194,9 +190,21 @@ module Rscons
@cache["directories"].keys
end
# Private Instance Methods
private
# Create a Cache object and load in the previous contents from the cache
# file.
def initialize!
@cache = JSON.load(File.read(CACHE_FILE)) rescue {}
unless @cache.is_a?(Hash)
$stderr.puts "Warning: #{CACHE_FILE} was corrupt. Contents:\n#{@cache.inspect}"
@cache = {}
end
@cache["targets"] ||= {}
@cache["directories"] ||= {}
@lookup_checksums = {}
end
# Return a file's checksum, or the previously calculated checksum for
# the same file
# @param file [String] The file name.
+2 -1
View File
@@ -172,7 +172,8 @@ module Rscons
# called after the block returns.
def process
clean_target_paths!
cache = Cache.new
cache = Cache.instance
cache.clear_checksum_cache!
targets_processed = {}
process_target = proc do |target|
targets_processed[target] ||= begin