Module: Solargraph::Cache

Defined in:
lib/solargraph/cache.rb

Class Method Summary collapse

Class Method Details

.base_dirString

The base directory where cached documentation is installed.

Returns:

  • (String)


10
11
12
13
14
15
16
# File 'lib/solargraph/cache.rb', line 10

def base_dir
  # The directory is not stored in a variable so it can be overridden
  # in specs.
  ENV['SOLARGRAPH_CACHE'] ||
    (ENV['XDG_CACHE_HOME'] ? File.join(ENV['XDG_CACHE_HOME'], 'solargraph') : nil) ||
    File.join(Dir.home, '.cache', 'solargraph')
end

.clearvoid

This method returns an undefined value.



72
73
74
# File 'lib/solargraph/cache.rb', line 72

def clear
  FileUtils.rm_rf base_dir, secure: true
end

.exist?(*path) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/solargraph/cache.rb', line 50

def exist? *path
  File.file? join(*path)
end

.join(*path) ⇒ String

Append the given path to the current cache directory (‘work_dir`).

Examples:

Cache.join('date-3.4.1.ser')

Parameters:

  • path (Array<String>)

Returns:

  • (String)


34
35
36
# File 'lib/solargraph/cache.rb', line 34

def join *path
  File.join(work_dir, *path)
end

.load(*path) ⇒ Array<Solargraph::Pin::Base>?

Parameters:

  • path (Array<String>)

Returns:



40
41
42
43
44
45
46
47
48
# File 'lib/solargraph/cache.rb', line 40

def load *path
  file = join(*path)
  return nil unless File.file?(file)
  Marshal.load(File.read(file, mode: 'rb'))
rescue StandardError => e
  Solargraph.logger.warn "Failed to load cached file #{file}: [#{e.class}] #{e.message}"
  FileUtils.rm_f file
  nil
end

.save(*path, pins) ⇒ void

This method returns an undefined value.

Parameters:

  • path (Array<String>)
  • pins (Array<Pin::Base>)


57
58
59
60
61
62
63
# File 'lib/solargraph/cache.rb', line 57

def save *path, pins
  file = File.join(work_dir, *path)
  base = File.dirname(file)
  FileUtils.mkdir_p base unless File.directory?(base)
  ser = Marshal.dump(pins)
  File.write file, ser, mode: 'wb'
end

.uncache(*path) ⇒ void

This method returns an undefined value.

Parameters:

  • path (Array<String>)


67
68
69
# File 'lib/solargraph/cache.rb', line 67

def uncache *path
  FileUtils.rm_rf File.join(work_dir, *path), secure: true
end

.work_dirString

The working directory for the current Ruby, RBS, and Solargraph versions.

Returns:

  • (String)


21
22
23
24
25
# File 'lib/solargraph/cache.rb', line 21

def work_dir
  # The directory is not stored in a variable so it can be overridden
  # in specs.
  File.join(base_dir, "ruby-#{RUBY_VERSION}", "rbs-#{RBS::VERSION}", "solargraph-#{Solargraph::VERSION}")
end