- 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.
37 lines
799 B
C++
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
|