mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-12 17:20:35 +08:00
Perform VACCUM once per week
This commit is contained in:
parent
333e2054d9
commit
b55a7decb9
@ -69,18 +69,33 @@ export class Cache {
|
|||||||
if (tbd != null) this.tbd = tbd;
|
if (tbd != null) this.tbd = tbd;
|
||||||
|
|
||||||
const db = new Database(path.join(this.cachePath, 'cache.db'));
|
const db = new Database(path.join(this.cachePath, 'cache.db'));
|
||||||
db.exec('PRAGMA journal_mode = WAL');
|
|
||||||
|
db.exec('PRAGMA journal_mode = WAL;');
|
||||||
|
db.exec('PRAGMA synchronous = normal;');
|
||||||
|
db.exec('PRAGMA temp_store = memory;');
|
||||||
|
db.exec('PRAGMA optimize;');
|
||||||
|
|
||||||
db.prepare('CREATE TABLE IF NOT EXISTS cache (key TEXT PRIMARY KEY, value TEXT, ttl REAL NOT NULL);').run();
|
db.prepare('CREATE TABLE IF NOT EXISTS cache (key TEXT PRIMARY KEY, value TEXT, ttl REAL NOT NULL);').run();
|
||||||
db.prepare('CREATE INDEX IF NOT EXISTS cache_ttl ON cache (ttl);').run();
|
db.prepare('CREATE INDEX IF NOT EXISTS cache_ttl ON cache (ttl);').run();
|
||||||
|
|
||||||
|
const date = new Date();
|
||||||
|
|
||||||
// perform purge on startup
|
// perform purge on startup
|
||||||
|
|
||||||
// ttl + tbd < now => ttl < now - tbd
|
// ttl + tbd < now => ttl < now - tbd
|
||||||
const now = Date.now() - this.tbd;
|
const now = date.getTime() - this.tbd;
|
||||||
db.prepare('DELETE FROM cache WHERE ttl < ?').run(now);
|
db.prepare('DELETE FROM cache WHERE ttl < ?').run(now);
|
||||||
|
|
||||||
this.db = db;
|
this.db = db;
|
||||||
|
|
||||||
|
const dateString = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
|
||||||
|
const lastVaccum = this.get('__LAST_VACUUM');
|
||||||
|
if (lastVaccum === undefined || (lastVaccum !== dateString && date.getUTCDay() === 6)) {
|
||||||
|
console.log(picocolors.magenta('[cache] vacuuming'));
|
||||||
|
|
||||||
|
this.set('__LAST_VACUUM', dateString, 10 * 365 * 60 * 60 * 24 * 1000);
|
||||||
|
this.db.exec('VACUUM;');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
set(key: string, value: string, ttl = 60 * 1000): void {
|
set(key: string, value: string, ttl = 60 * 1000): void {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user