From ffb4123bb4063ca954c23f08daeedf2f571c4fdc Mon Sep 17 00:00:00 2001 From: welefen Date: Wed, 2 Nov 2016 15:57:19 +0800 Subject: [PATCH] Try 3 times when can not get file content --- src/adapter/store/file.js | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/src/adapter/store/file.js b/src/adapter/store/file.js index 41698eae..82e93325 100644 --- a/src/adapter/store/file.js +++ b/src/adapter/store/file.js @@ -3,12 +3,6 @@ import fs from 'fs'; import path from 'path'; -/** - * lock files - * @type {Object} - */ -let lockFiles = {}; - /** * file store class */ @@ -44,14 +38,18 @@ export default class extends think.adapter.base { * @param {String} key [] * @return {Promise} [] */ - async get(key){ + get(key, times = 1){ let filePath = this.getFilePath(key); - if(!think.isFile(filePath)){ + if(times === 1 && !think.isFile(filePath)){ return Promise.resolve(); } - - await lockFiles[filePath]; - return think.promisify(fs.readFile, fs)(filePath, {encoding: 'utf8'}); + // try 3 times when can not get file content + return think.promisify(fs.readFile, fs)(filePath, {encoding: 'utf8'}).then(content => { + if(!content && times <= 3){ + return this.get(key, times + 1); + } + return content; + }); } /** * set file content @@ -63,12 +61,9 @@ export default class extends think.adapter.base { think.mkdir(path.dirname(filePath)); let fn = think.promisify(fs.writeFile, fs); - let promise = fn(filePath, content).then(() => { + return fn(filePath, content).then(() => { think.chmod(filePath); - delete lockFiles[filePath]; }); - lockFiles[filePath] = promise; - return promise; } /** * delete file