kura
Version:
The FileSystem API abstraction library.
72 lines • 2.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ContentsCache = void 0;
const FileSystemConstants_1 = require("./FileSystemConstants");
const FileSystemUtil_1 = require("./FileSystemUtil");
class ContentsCache {
constructor(accessor) {
this.cache = {};
this.options = accessor.options.contentsCacheOptions;
}
clear() {
this.cache = {};
}
get(fullPath) {
const entry = this.cache[fullPath];
if (!entry) {
return null;
}
entry.access = Date.now();
return entry.content;
}
put(obj, content) {
const fullPath = obj.fullPath;
if (fullPath.startsWith(FileSystemConstants_1.INDEX_DIR_PATH)) {
return;
}
delete this.cache[fullPath];
const size = (0, FileSystemUtil_1.getMemorySize)(content);
if (size === 0 || this.options.limitSize < size) {
return;
}
const capacity = this.options.capacity;
let sum = 0;
const list = [];
for (const [fullPath, entry] of Object.entries(this.cache)) {
sum += entry.size;
list.push({ fullPath, size: entry.size, access: entry.access });
}
let current = sum + size;
if (current <= capacity) {
this.cache[fullPath] = {
content,
size,
lastModified: obj.lastModified,
access: Date.now(),
};
return;
}
list.sort((a, b) => {
return b.access - a.access;
});
const limit = capacity - size;
for (const item of list) {
delete this.cache[item.fullPath];
current -= item.size;
if (current <= limit) {
break;
}
}
this.cache[fullPath] = {
content,
size,
lastModified: obj.lastModified,
access: Date.now(),
};
}
remove(fullPath) {
delete this.cache[fullPath];
}
}
exports.ContentsCache = ContentsCache;
//# sourceMappingURL=ContentsCache.js.map