module Solargraph::Cache

def base_dir

Returns:
  • (String) -
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

def clear

Returns:
  • (void) -
def clear
  FileUtils.rm_rf base_dir, secure: true
end

def exist? *path

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

def join *path

Returns:
  • (String) -

Parameters:
  • path (Array) --
def join *path
  File.join(work_dir, *path)
end

def load *path

Returns:
  • (Array, nil) -

Parameters:
  • path (Array) --
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

def save *path, pins

Returns:
  • (void) -

Parameters:
  • pins (Array) --
  • path (Array) --
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

def uncache *path

Parameters:
  • path (Array) --

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

def work_dir

Returns:
  • (String) -
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