code cleanup
This commit is contained in:
parent
4680d7f014
commit
d21b873327
12 changed files with 40 additions and 213 deletions
|
|
@ -42,7 +42,6 @@ function createHashCache(dataDir) {
|
|||
let cache = null
|
||||
let dirty = false
|
||||
let savedAt = 0
|
||||
const stats = { hits: 0, misses: 0 }
|
||||
|
||||
function load() {
|
||||
if (cache) return cache
|
||||
|
|
@ -80,11 +79,7 @@ function createHashCache(dataDir) {
|
|||
// the cache dirty only when the timestamp is stale.
|
||||
function hit(rel, size, mtimeMs) {
|
||||
const known = load()[rel]
|
||||
if (!known || known[0] !== size || known[1] !== mtimeMs || typeof known[2] !== 'string') {
|
||||
stats.misses++
|
||||
return null
|
||||
}
|
||||
stats.hits++
|
||||
if (!known || known[0] !== size || known[1] !== mtimeMs || typeof known[2] !== 'string') return null
|
||||
if (Date.now() - (known[3] || 0) > 86400000) {
|
||||
known[3] = Date.now()
|
||||
dirty = true
|
||||
|
|
@ -111,35 +106,13 @@ function createHashCache(dataDir) {
|
|||
cache = null
|
||||
dirty = false
|
||||
savedAt = 0
|
||||
stats.hits = 0
|
||||
stats.misses = 0
|
||||
try {
|
||||
fs.rmSync(FILE, { force: true })
|
||||
fs.rmSync(`${FILE}.tmp`, { force: true })
|
||||
} catch {}
|
||||
}
|
||||
|
||||
// Dropping the memory copy makes the next lookup re-read from disk (tests).
|
||||
function forget() {
|
||||
cache = null
|
||||
dirty = false
|
||||
}
|
||||
|
||||
return {
|
||||
HASH_ALGO,
|
||||
hashFile,
|
||||
hit,
|
||||
remember,
|
||||
cachedHash,
|
||||
save,
|
||||
clear,
|
||||
forget,
|
||||
stats: () => ({ ...stats, size: cache ? Object.keys(cache).length : 0, algo: HASH_ALGO }),
|
||||
resetStats: () => {
|
||||
stats.hits = 0
|
||||
stats.misses = 0
|
||||
}
|
||||
}
|
||||
return { HASH_ALGO, hashFile, hit, remember, cachedHash, save, clear }
|
||||
}
|
||||
|
||||
module.exports = { createHashCache, hashFile, HASH_ALGO }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue