Class: PuppetLanguageServerSidecar::Cache::FileSystem

Inherits:
Base
  • Object
show all
Defined in:
lib/puppet-languageserver-sidecar/cache/filesystem.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#cache_options

Instance Method Summary collapse

Constructor Details

#initialize(_options = {}) ⇒ FileSystem

Returns a new instance of FileSystem.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/puppet-languageserver-sidecar/cache/filesystem.rb', line 8

def initialize(_options = {})
  super
  require 'digest'
  require 'json'
  require 'tmpdir'

  @cache_dir = File.join(Dir.tmpdir, 'puppet-vscode-cache')
  begin
    Dir.mkdir(@cache_dir) unless Dir.exist?(@cache_dir)
  rescue Errno::ENOENT => e
    PuppetLanguageServerSidecar.log_message(:error, "[PuppetLanguageServerSidecar::Cache::FileSystem] An error occured while creating file cache.  Disabling cache: #{e}")
    @cache_dir = nil
  end
end

Instance Attribute Details

#cache_dirObject (readonly)

Returns the value of attribute cache_dir.



6
7
8
# File 'lib/puppet-languageserver-sidecar/cache/filesystem.rb', line 6

def cache_dir
  @cache_dir
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/puppet-languageserver-sidecar/cache/filesystem.rb', line 23

def active?
  !@cache_dir.nil?
end

#clear!Object



76
77
78
79
80
81
# File 'lib/puppet-languageserver-sidecar/cache/filesystem.rb', line 76

def clear!
  return unless active?

  PuppetLanguageServerSidecar.log_message(:warn, '[PuppetLanguageServerSidecar::Cache::FileSystem.clear] Filesystem based cache is being cleared')
  FileUtils.rm(Dir.glob(File.join(cache_dir, '*')), force: true)
end

#load(absolute_path, section) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/puppet-languageserver-sidecar/cache/filesystem.rb', line 27

def load(absolute_path, section)
  return nil unless active?

  file_key = file_key(absolute_path, section)
  cache_file = File.join(cache_dir, cache_filename(file_key))

  content = read_file(cache_file)
  return nil if content.nil?

  json_obj = JSON.parse(content)
  return nil if json_obj.nil?

  # Check that this is from the same language server version
  unless json_obj['sidecar_version'] == PuppetLanguageServerSidecar.version
    PuppetLanguageServerSidecar.log_message(:debug, "[PuppetLanguageServerSidecar::Cache::FileSystem.load] Error loading #{absolute_path}: Expected sidecar_version version #{PuppetLanguageServerSidecar.version} but found #{json_obj['sidecar_version']}")
    return nil
  end
  # Check that the source file hash matches
  content_hash = calculate_hash(absolute_path)
  if json_obj['file_hash'] != content_hash
    PuppetLanguageServerSidecar.log_message(:debug, "[PuppetLanguageServerSidecar::Cache::FileSystem.load] Error loading #{absolute_path}: Expected file_hash of #{content_hash} but found #{json_obj['file_hash']}")
    return nil
  end
  PuppetLanguageServerSidecar.log_message(:debug, "[PuppetLanguageServerSidecar::Cache::FileSystem.load] Loading #{absolute_path} from cache")

  json_obj['data']
rescue RuntimeError => e
  PuppetLanguageServerSidecar.log_message(:debug, "[PuppetLanguageServerSidecar::Cache::FileSystem.load] Error loading #{absolute_path}: #{e}")
  raise
end

#save(absolute_path, section, content_string) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/puppet-languageserver-sidecar/cache/filesystem.rb', line 58

def save(absolute_path, section, content_string)
  return false unless active?

  file_key = file_key(absolute_path, section)
  cache_file = File.join(cache_dir, cache_filename(file_key))

  content = { 'data' => content_string }
  # Inject metadata
  content['sidecar_version'] = PuppetLanguageServerSidecar.version
  content['file_hash'] = calculate_hash(absolute_path)
  content['created'] = Time.now.utc.strftime('%FT%T')
  content['path'] = absolute_path
  content['section'] = section

  PuppetLanguageServerSidecar.log_message(:debug, "[PuppetLanguageServerSidecar::Cache::FileSystem.save] Saving #{absolute_path} to cache")
  save_file(cache_file, content.to_json)
end