diff options
author | Eelco Dolstra <edolstra@gmail.com> | 2017-01-27 15:19:33 +0100 |
---|---|---|
committer | Eelco Dolstra <edolstra@gmail.com> | 2017-01-27 15:19:33 +0100 |
commit | cb1951e746cb04f331b126c746758c96bb9fdc81 (patch) | |
tree | 6a4668f294e0c1fe32faaa80d6b3c3b55623d482 /src/libstore/nar-info-disk-cache.cc | |
parent | 211bc7f0e6daa65fe4083334e2411bc441067904 (diff) |
Periodically purge binary-cache.sqlite
Diffstat (limited to 'src/libstore/nar-info-disk-cache.cc')
-rw-r--r-- | src/libstore/nar-info-disk-cache.cc | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/src/libstore/nar-info-disk-cache.cc b/src/libstore/nar-info-disk-cache.cc index ed2f18ffe..13b67b81f 100644 --- a/src/libstore/nar-info-disk-cache.cc +++ b/src/libstore/nar-info-disk-cache.cc @@ -36,6 +36,11 @@ create table if not exists NARs ( foreign key (cache) references BinaryCaches(id) on delete cascade ); +create table if not exists LastPurge ( + dummy text primary key, + value integer +); + )sql"; class NarInfoDiskCacheImpl : public NarInfoDiskCache @@ -46,6 +51,9 @@ public: const int ttlNegative = 3600; const int ttlPositive = 30 * 24 * 3600; + /* How often to purge expired entries from the cache. */ + const int purgeInterval = 24 * 3600; + struct Cache { int id; @@ -57,7 +65,7 @@ public: struct State { SQLite db; - SQLiteStmt insertCache, queryCache, insertNAR, insertMissingNAR, queryNAR; + SQLiteStmt insertCache, queryCache, insertNAR, insertMissingNAR, queryNAR, purgeCache; std::map<std::string, Cache> caches; }; @@ -96,6 +104,27 @@ public: state->queryNAR.create(state->db, "select * from NARs where cache = ? and hashPart = ? and ((present = 0 and timestamp > ?) or (present = 1 and timestamp > ?))"); + + /* Periodically purge expired entries from the database. */ + auto now = time(0); + + SQLiteStmt queryLastPurge(state->db, "select value from LastPurge"); + auto queryLastPurge_(queryLastPurge.use()); + + if (!queryLastPurge_.next() || queryLastPurge_.getInt(0) < now - purgeInterval) { + SQLiteStmt(state->db, + "delete from NARs where ((present = 0 and timestamp < ?) or (present = 1 and timestamp < ?))") + .use() + (now - ttlNegative) + (now - ttlPositive) + .exec(); + + debug("deleted %d entries from the NAR info disk cache", sqlite3_changes(state->db)); + + SQLiteStmt(state->db, + "insert or replace into LastPurge(dummy, value) values ('', ?)") + .use()(now).exec(); + } } Cache & getCache(State & state, const std::string & uri) |