From 0b9a27fd218293d67e4b004219b0c0121c6635b8 Mon Sep 17 00:00:00 2001 From: zenboss Date: Sun, 5 Jul 2015 12:10:50 +0800 Subject: [PATCH] optimize code --- Readme.md | 13 +++++---- index.js | 74 +++++++++++++++++++++++++++------------------------- package.json | 2 +- test/test.js | 2 +- 4 files changed, 48 insertions(+), 43 deletions(-) diff --git a/Readme.md b/Readme.md index a304bd4..87eb4ec 100644 --- a/Readme.md +++ b/Readme.md @@ -20,16 +20,19 @@ This is a key&value storage library, which uses file system to store data. var filekv = require('filekv'); var filekvClient = new filekv({ + //Here is your datafile dir! + //!!!This config item not have default value!!! + fileDir:__dirname+'/data', - fileDir:__dirname+'/data', //Here is your datafile dir! This config item not have default value!!! - - workQueueMax:1000 //This number can't greater than your OS open file max number! + //This number can't greater than your OS open file max number! + //This config item default is 1000 + workMax:1000 }); -filekvClient.set('userinfo',{name:'wzy',sex:1,github:'http://www.github.com/zenboss'},(Date.now()/1000)+3600,function(err){ +filekvClient.set('userinfo',{name:'wzy',sex:1,github:'http://www.github.com/zenboss'},3600,function(err){ console.log(err); @@ -57,7 +60,7 @@ filekvClient.del('userinfo',function(err){ # API -### filekv.prototype.set(key,value/object[,expireTime[,callback]]) +### filekv.prototype.set(key,value/object[,lifeTime[,callback]]) >Use this function setting a key&value diff --git a/index.js b/index.js index 6faff96..7fce9b7 100644 --- a/index.js +++ b/index.js @@ -168,52 +168,54 @@ filekv.prototype.get = function(key,opt,cb){ }; -filekv.prototype.set = function(key,value,expireTime,opt,cb){ +filekv.prototype.set = function(key,value,lifeTime,opt,cb){ var self = this; - if('function' == typeof expireTime){ - cb = expireTime; - expireTime = 0; + if('function' == typeof lifeTime){ + cb = lifeTime; + lifeTime = 0; } if('function' == typeof opt){ cb = opt; opt = {}; } - opt = opt||{}; - cb = cb||function(){}; + opt = opt||{}; + cb = cb||function(){}; + lifeTime = parseInt(lifeTime)||0; - var md5key = _md5(key); - var filePath = this.fileDir+'/'+_getDataFileSubDir(md5key)+'/'; - - - self.tool.mkdirs(filePath,function(err){//TODO : this callback function's arguments need check error - if(!!err){ - cb(err); - return; - } - var fileAllPath = filePath+'/'+md5key+'.fkv'; - var createTime = parseInt(Date.now()/1000); - expireTime = parseInt(expireTime)||0; - if(expireTime!=0)expireTime += createTime; - - var fileData = ''; - fileData += expireTime+'\n'; - fileData += createTime+'\n'; - fileData += JSON.stringify(value); - self._workQueue.queue(function(queueCB){ - - fs.writeFile(fileAllPath,fileData,function(err){ - cb.apply(self,arguments); - queueCB.apply(self,arguments); - - if(expireTime!=0 && expireTime<=createTime){ - self.del(key); - } - }); + var md5key = _md5(key); + var filePath = this.fileDir+'/'+_getDataFileSubDir(md5key)+'/'; + + + self._workQueue.queue(function(queueCB){ + self.tool.mkdirs(filePath,function(err){ + if(!!err){ + queueCB(err); + cb(err); + return; + } + var fileAllPath = filePath+'/'+md5key+'.fkv'; + var createTime = parseInt(Date.now()/1000); + var expireTime = 0; + + if(lifeTime!=0)expireTime = createTime + lifeTime; + + var fileData = ''; + fileData += expireTime+'\n'; + fileData += createTime+'\n'; + fileData += JSON.stringify(value); + + fs.writeFile(fileAllPath,fileData,function(err){ + queueCB.apply(self,arguments); + cb.apply(self,arguments); + + if(expireTime!=0 && expireTime<=createTime){ + self.del(key); + } + }); }); - - }); + }); }; diff --git a/package.json b/package.json index 1fe7c0b..bb79209 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "filekv", - "version": "1.0.7", + "version": "1.0.8", "description": "This is a key&value storage library, which uses file system to store data.", "main": "index.js", "directories": { diff --git a/test/test.js b/test/test.js index a9f8178..49311a6 100644 --- a/test/test.js +++ b/test/test.js @@ -9,7 +9,7 @@ var fkvObj = require('../index.js').create({ }); -for(var i =0;i<1000;i++){ +for(var i =0;i<90000;i++){ (function(){ var innerI = i;