|
| 1 | +module PuppetLanguageServer |
| 2 | + module PuppetHelper |
| 3 | + class PersistentFileCache |
| 4 | + attr_reader :cache_dir |
| 5 | + |
| 6 | + def initialize(_inmemory_cache, _options = {}) |
| 7 | + require 'digest' |
| 8 | + require 'json' |
| 9 | + |
| 10 | + @cache_dir = File.join(ENV['TMP'], 'puppet-vscode-cache') |
| 11 | + Dir.mkdir(@cache_dir) unless Dir.exist?(@cache_dir) |
| 12 | + end |
| 13 | + |
| 14 | + def load(absolute_path, file_key) |
| 15 | + cache_file = File.join(cache_dir, cache_filename(file_key)) |
| 16 | + |
| 17 | + content = read_cache_file(cache_file) |
| 18 | + return nil if content.nil? |
| 19 | + |
| 20 | + json_obj = JSON.parse(content) |
| 21 | + |
| 22 | + # Check that this is from the same language server version |
| 23 | + unless json_obj.nil? || json_obj['metadata']['version'] == PuppetVSCode.version |
| 24 | + PuppetLanguageServer.log_message(:debug, "[PuppetHelperFileCache::load_from_persistent_cache] Error loading #{absolute_path}: Expected language server version #{PuppetVSCode.version} but found #{json_obj['metadata']['version']}") |
| 25 | + json_obj = nil |
| 26 | + end |
| 27 | + # Check that the source file hash matches |
| 28 | + content_hash = calculate_hash(absolute_path) unless json_obj.nil? |
| 29 | + if json_obj.nil? || json_obj['metadata']['content_hash'] != content_hash |
| 30 | + PuppetLanguageServer.log_message(:debug, "[PuppetHelperFileCache::load_from_persistent_cache] Error loading #{absolute_path}: Expected content_hash of #{content_hash} but found #{json_obj['metadata']['content_hash']}") |
| 31 | + json_obj = nil |
| 32 | + else |
| 33 | + PuppetLanguageServer.log_message(:debug, "[PuppetHelperFileCache::load_from_persistent_cache] Loading #{absolute_path} from cache") |
| 34 | + end |
| 35 | + |
| 36 | + json_obj |
| 37 | + rescue RuntimeError => detail |
| 38 | + PuppetLanguageServer.log_message(:debug, "[PuppetHelperFileCache::load_from_persistent_cache] Error loading #{absolute_path}: #{detail}") |
| 39 | + raise |
| 40 | + end |
| 41 | + |
| 42 | + def save!(absolute_path, content) |
| 43 | + file_key = canonical_path(absolute_path) |
| 44 | + cache_file = File.join(cache_dir, cache_filename(file_key)) |
| 45 | + |
| 46 | + # Inject metadata |
| 47 | + content['metadata']['version'] = PuppetVSCode.version |
| 48 | + content['metadata']['content_hash'] = calculate_hash(absolute_path) |
| 49 | + |
| 50 | + save_cache_file(cache_file, content.to_json) |
| 51 | + end |
| 52 | + |
| 53 | + private |
| 54 | + |
| 55 | + def save_cache_file(filepath, content) |
| 56 | + File.open(filepath, 'wb') { |file| file.write(content) } |
| 57 | + true |
| 58 | + end |
| 59 | + |
| 60 | + def read_cache_file(filepath) |
| 61 | + return nil unless File.exist?(filepath) |
| 62 | + |
| 63 | + File.open(filepath, 'rb') { |file| file.read } |
| 64 | + end |
| 65 | + |
| 66 | + def cache_filename(absolute_path) |
| 67 | + Digest::SHA256.hexdigest(canonical_path(absolute_path)) + '.txt' |
| 68 | + end |
| 69 | + |
| 70 | + def calculate_hash(filepath) |
| 71 | + Digest::SHA256.hexdigest(read_cache_file(filepath)) |
| 72 | + end |
| 73 | + |
| 74 | + def canonical_path(filepath) |
| 75 | + # Strictly speaking some file systems are case sensitive but ruby/puppet throws a fit |
| 76 | + # with naming if you do |
| 77 | + file_key = filepath.downcase |
| 78 | + |
| 79 | + file_key |
| 80 | + end |
| 81 | + end |
| 82 | + end |
| 83 | +end |
0 commit comments