File size: 995 Bytes
1fa2c88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import sqlite3 from 'sqlite3';
import sqliteStore from 'cache-manager-sqlite';
import cacheManager from 'cache-manager';
import config from './config.js';
import {wait} from './util.js';

const db = new sqlite3.Database(`${config.dataFolder}/cache.db`);

const cache = await cacheManager.caching({
  store: sqliteStore,
  path: `${config.dataFolder}/cache.db`,
  options: { ttl: 86400 }
});

export default cache;

export async function clean(){
  // https://github.com/maxpert/node-cache-manager-sqlite/blob/36a1fe44a30b6af8d8c323c59e09fe81bde539d9/index.js#L146
  // The cache will grow until an expired key is requested
  // This hack should force node-cache-manager-sqlite to purge
  await cache.set('_clean', 'todo', {ttl: 1});
  await wait(3e3);
  await cache.get('_clean');
}

export async function vacuum(){
  return new Promise((resolve, reject) => {
    db.serialize(() => {
      db.run('VACUUM', err => {
        if(err)return reject(err);
        resolve();
      })
    });
  });
}