mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2025-02-27 20:57:17 +02:00
Fetcher cache: Add support for caching facts not related to store paths
This commit is contained in:
parent
fa6bc33604
commit
e1b8442fa1
2 changed files with 95 additions and 0 deletions
|
@ -19,6 +19,9 @@ create table if not exists Cache (
|
||||||
);
|
);
|
||||||
)sql";
|
)sql";
|
||||||
|
|
||||||
|
// FIXME: we should periodically purge/nuke this cache to prevent it
|
||||||
|
// from growing too big.
|
||||||
|
|
||||||
struct CacheImpl : Cache
|
struct CacheImpl : Cache
|
||||||
{
|
{
|
||||||
struct State
|
struct State
|
||||||
|
@ -47,6 +50,60 @@ struct CacheImpl : Cache
|
||||||
"select info, path, immutable, timestamp from Cache where input = ?");
|
"select info, path, immutable, timestamp from Cache where input = ?");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void upsert(
|
||||||
|
const Attrs & inAttrs,
|
||||||
|
const Attrs & infoAttrs) override
|
||||||
|
{
|
||||||
|
_state.lock()->add.use()
|
||||||
|
(attrsToJSON(inAttrs).dump())
|
||||||
|
(attrsToJSON(infoAttrs).dump())
|
||||||
|
("") // no path
|
||||||
|
(false)
|
||||||
|
(time(0)).exec();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<Attrs> lookup(const Attrs & inAttrs) override
|
||||||
|
{
|
||||||
|
if (auto res = lookupExpired(inAttrs))
|
||||||
|
return std::move(res->infoAttrs);
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<Attrs> lookupWithTTL(const Attrs & inAttrs) override
|
||||||
|
{
|
||||||
|
if (auto res = lookupExpired(inAttrs)) {
|
||||||
|
if (!res->expired)
|
||||||
|
return std::move(res->infoAttrs);
|
||||||
|
debug("ignoring expired cache entry '%s'",
|
||||||
|
attrsToJSON(inAttrs).dump());
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<Result2> lookupExpired(const Attrs & inAttrs) override
|
||||||
|
{
|
||||||
|
auto state(_state.lock());
|
||||||
|
|
||||||
|
auto inAttrsJSON = attrsToJSON(inAttrs).dump();
|
||||||
|
|
||||||
|
auto stmt(state->lookup.use()(inAttrsJSON));
|
||||||
|
if (!stmt.next()) {
|
||||||
|
debug("did not find cache entry for '%s'", inAttrsJSON);
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
auto infoJSON = stmt.getStr(0);
|
||||||
|
auto locked = stmt.getInt(2) != 0;
|
||||||
|
auto timestamp = stmt.getInt(3);
|
||||||
|
|
||||||
|
debug("using cache entry '%s' -> '%s'", inAttrsJSON, infoJSON);
|
||||||
|
|
||||||
|
return Result2 {
|
||||||
|
.expired = !locked && (settings.tarballTtl.get() == 0 || timestamp + settings.tarballTtl < time(0)),
|
||||||
|
.infoAttrs = jsonToAttrs(nlohmann::json::parse(infoJSON)),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
void add(
|
void add(
|
||||||
ref<Store> store,
|
ref<Store> store,
|
||||||
const Attrs & inAttrs,
|
const Attrs & inAttrs,
|
||||||
|
|
|
@ -10,6 +10,44 @@ struct Cache
|
||||||
{
|
{
|
||||||
virtual ~Cache() { }
|
virtual ~Cache() { }
|
||||||
|
|
||||||
|
/* A cache for arbitrary Attrs -> Attrs mappings with a timestamp
|
||||||
|
for expiration. */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Add a value to the cache. The cache is an arbitrary mapping of
|
||||||
|
* Attrs to Attrs.
|
||||||
|
*/
|
||||||
|
virtual void upsert(
|
||||||
|
const Attrs & inAttrs,
|
||||||
|
const Attrs & infoAttrs) = 0;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Look up a key with infinite TTL.
|
||||||
|
*/
|
||||||
|
virtual std::optional<Attrs> lookup(
|
||||||
|
const Attrs & inAttrs) = 0;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Look up a key. Return nothing if its TTL has exceeded
|
||||||
|
* `settings.tarballTTL`.
|
||||||
|
*/
|
||||||
|
virtual std::optional<Attrs> lookupWithTTL(
|
||||||
|
const Attrs & inAttrs) = 0;
|
||||||
|
|
||||||
|
struct Result2
|
||||||
|
{
|
||||||
|
bool expired = false;
|
||||||
|
Attrs infoAttrs;
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Look up a key. Return a bool denoting whether its TTL has
|
||||||
|
* exceeded `settings.tarballTTL`.
|
||||||
|
*/
|
||||||
|
virtual std::optional<Result2> lookupExpired(
|
||||||
|
const Attrs & inAttrs) = 0;
|
||||||
|
|
||||||
|
/* Old cache for things that have a store path. */
|
||||||
virtual void add(
|
virtual void add(
|
||||||
ref<Store> store,
|
ref<Store> store,
|
||||||
const Attrs & inAttrs,
|
const Attrs & inAttrs,
|
||||||
|
|
Loading…
Add table
Reference in a new issue