本來寫的誤操做被吞了,下面是代碼記錄
這個模塊主要作了緩存文件操做緩存
module.exports = class CachedInputFileSystem { constructor(fileSystem, duration) { this.fileSystem = fileSystem; //生成緩存容器,目前他們是同樣樣的都是new Storage(duration),區別在於調用方法傳遞參數不同,分別儲存不一個類型的緩存 this._statStorage = new Storage(duration); this._readdirStorage = new Storage(duration); this._readFileStorage = new Storage(duration); this._readJsonStorage = new Storage(duration); this._readlinkStorage = new Storage(duration); // 把文件信息賦值給對應屬性 this._stat = this.fileSystem.stat ? this.fileSystem.stat.bind(this.fileSystem) : null; if(!this._stat) this.stat = null; //... if(this.fileSystem.readJson) { this._readJson = this.fileSystem.readJson.bind(this.fileSystem); } else if(this.readFile) { // 自定義JSON讀取 this._readJson = (path, callback) => { // fs.readFile讀取文件 this.readFile(path, (err, buffer) => { if(err) return callback(err); let data; try { data = JSON.parse(buffer.toString("utf-8")); } catch(e) { return callback(e); } callback(null, data); }); }; } else { this.readJson = null; } //... } //調用Storage對象方法 stat(path, callback) { this._statStorage.provide(path, this._stat, callback); } //... readlinkSync(path) { return this._readlinkStorage.provideSync(path, this._readlinkSync); } //分別清除對應的Storage,what是傳入什麼,清除什麼,purge的中文的意思又整肅的意思,和clear有點區別,有完整清除不能被回退的感受 purge(what) { this._statStorage.purge(what); this._readdirStorage.purge(what); this._readFileStorage.purge(what); this._readlinkStorage.purge(what); this._readJsonStorage.purge(what); } };