Files
nix/src/libutil/nar-cache.cc
John Ericson bec4da9e26 NarCache improvements
- Separate implementation, abstract implements

- `NarCache` work in constant memory

  This is using `RestoreSink` to dedup some write-side IO, but I am not
  so sure that is a good idea. We'll continue reworking it.

- Use pathlocks to avoid download storms

- Use `Descriptor`-based logic, not `pathExists` to avoid symlink races
  if anything is deleted.
2026-01-26 15:30:09 -05:00

37 lines
799 B
C++

#include "nix/util/nar-cache.hh"
namespace nix {
namespace {
/**
* In-memory only NAR cache (private implementation).
*/
class MemoryNarCache : public NarCache
{
public:
ref<NarAccessor> getOrInsert(const Hash & narHash, std::function<void(Sink &)> populate) override;
};
} // anonymous namespace
ref<NarAccessor> MemoryNarCache::getOrInsert(const Hash & narHash, std::function<void(Sink &)> populate)
{
// Check in-memory cache first
if (auto * accessor = get(nars, narHash))
return *accessor;
StringSink sink;
populate(sink);
auto accessor = makeNarAccessor(std::move(sink.s));
nars.emplace(narHash, accessor);
return accessor;
}
std::unique_ptr<NarCache> makeMemoryNarCache()
{
return std::make_unique<MemoryNarCache>();
}
} // namespace nix