clang-tools 22.0.0git
clang::clangd::FileCache Class Reference

Base class for threadsafe cache of data read from a file on disk. More...

#include <FileCache.h>

Inheritance diagram for clang::clangd::FileCache:
[legend]

Protected Member Functions

 FileCache (PathRef Path)
void read (const ThreadsafeFS &TFS, std::chrono::steady_clock::time_point FreshTime, llvm::function_ref< void(std::optional< llvm::StringRef >)> Parse, llvm::function_ref< void()> Read) const
PathRef path () const

Detailed Description

Base class for threadsafe cache of data read from a file on disk.

We want configuration files to be "live" as much as possible. Reading them every time is simplest, but caching solves a few problems:

  • reading and parsing is cheap but not free (and happens on hot paths)
  • we can ignore invalid data and use the old value (we may see truncated compile_commands.json from non-atomic writers)
  • we avoid reporting the same errors repeatedly

We still read and parse the data synchronously on demand, but skip as much work as possible:

  • if not enough wall-time has elapsed, assume the data is still up-to-date
  • if we stat the file and it has the same mtime + size, don't read it
  • obviously we only have to parse when we re-read the file (Tracking OS change events is an alternative, but difficult to do portably.)

Caches for particular data (e.g. compilation databases) should inherit and:

  • add mutable storage for the cached parsed data
  • add a public interface implemented on top of read()

Definition at line 40 of file FileCache.h.

Constructor & Destructor Documentation

◆ FileCache()

clang::clangd::FileCache::FileCache ( PathRef Path)
protected

Member Function Documentation

◆ path()

PathRef clang::clangd::FileCache::path ( ) const
inlineprotected

Definition at line 63 of file FileCache.h.

Referenced by clang::clangd::config::FileConfigCache::get().

◆ read()

void clang::clangd::FileCache::read ( const ThreadsafeFS & TFS,
std::chrono::steady_clock::time_point FreshTime,
llvm::function_ref< void(std::optional< llvm::StringRef >)> Parse,
llvm::function_ref< void()> Read ) const
protected

The documentation for this class was generated from the following files: