this.unzipIfNeeded(data, function(err,data) {
                    try {
                        if(err){ // if unzippable - throw to remove
                            throw Error('unzippable: ' + err);
                        }
                        // get the json out of the data
                        var diskdata = JSON.parse(data);

                    } catch (err) {

                        // when the deserialize doesn't work, probably the file is uncomplete - so we delete it and ignore the error
                        try {
                            fs.unlinksync(filename);
                            // unlink binary
                            glob(filename.replace(/\.dat$/, '*.bin'), function (err, result) {
                                if (!err) {
                                    async.each(result, fs.unlink);
                                }
                            });
                        } catch (ignore) {

                        }

                        return callback();
                    }

                    // update the size in the metadata - this value isn't correctly stored in the file
                    // diskdata.size = data.length;

                    // update collection size
                    this.currentsize += diskdata.size;

                    // remove the entrys content - we don't want the content in the memory (only the meta informations)
                    diskdata.value = null;
                    delete diskdata.value;

                    // and put the entry in the store
                    this.collection[diskdata.key] = diskdata;

                    // check for expiry - in this case we instantly delete the entry
                    if (diskdata.expires < new Date()) {

                        this.del(diskdata.key, function () {

                            return callback();
                        });
                    } else {

                        return callback();
                    }
                }.bind(this));
示例#2
0
		  fs.readFile(filename, function (err, data) {

				// stop file processing when there was an reading error
				if (err) {
				  return callback();
				}

				try {

				  // get the json out of the data
				  var diskdata = JSON.parse(data);

				} catch(err) {

				  // when the deserialize doesn't work, probably the file is uncomplete - so we delete it and ignore the error
				  try {
				  	fs.unlinksync(filename);
				  } catch(ignore) {

				  }

				  return callback();
				}

				// update the size in the metadata - this value isn't correctly stored in the file
				diskdata.size = data.length;

				// update collection size
				this.currentsize+=data.length;

				// remove the entrys content - we don't want the content in the memory (only the meta informations)
				diskdata.value = null;
				delete diskdata.value;

				// and put the entry in the store
				this.collection[diskdata.key] = diskdata;

				// check for expiry - in this case we instantly delete the entry
				if (diskdata.expires < new Date()) {

				  this.del(diskdata.key, function () {

						return callback();
				  });
				} else {

				  return callback();
				}
		  }.bind(this));