Skip to content

Commit

Permalink
optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
zenboss committed Jul 5, 2015
1 parent 2b1834d commit 0b9a27f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 43 deletions.
13 changes: 8 additions & 5 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand Down
74 changes: 38 additions & 36 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});

});

});

});
};


Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 0b9a27f

Please sign in to comment.