Skip to content

Commit

Permalink
v2.0.0 - ES6 updates
Browse files Browse the repository at this point in the history
  • Loading branch information
cjihrig committed Oct 31, 2015
1 parent dc52ab4 commit 0dbbd16
Show file tree
Hide file tree
Showing 4 changed files with 289 additions and 267 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
language: node_js

node_js:
- 0.10
- 4.0
- 4
- 5

sudo: false

61 changes: 31 additions & 30 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
'use strict';

// Load modules

var Hoek = require('hoek');
const Hoek = require('hoek');


// Declare internals

var internals = {};
const internals = {};


internals.defaults = {
Expand All @@ -19,7 +21,7 @@ internals.MemoryCacheSegment = function MemoryCacheSegment () {

internals.MemoryCacheEntry = function MemoryCacheEntry (key, value, ttl, allowMixedContent) {

var valueByteSize = 0;
let valueByteSize = 0;

if (allowMixedContent && Buffer.isBuffer(value)) {
this.item = new Buffer(value.length);
Expand Down Expand Up @@ -71,12 +73,12 @@ internals.Connection.prototype.stop = function () {

// Clean up pending eviction timers.
if (this.cache) {
var segments = Object.keys(this.cache);
for (var i = 0, il = segments.length; i < il; ++i) {
var segment = segments[i];
var keys = Object.keys(this.cache[segment]);
for (var j = 0, jl = keys.length; j < jl; ++j) {
var key = keys[j];
const segments = Object.keys(this.cache);
for (let i = 0; i < segments.length; ++i) {
const segment = segments[i];
const keys = Object.keys(this.cache[segment]);
for (let j = 0; j < keys.length; ++j) {
const key = keys[j];
clearTimeout(this.cache[segment][key].timeoutId);
}
}
Expand All @@ -90,7 +92,7 @@ internals.Connection.prototype.stop = function () {

internals.Connection.prototype.isReady = function () {

return (!!this.cache);
return !!this.cache;
};


Expand All @@ -116,17 +118,19 @@ internals.Connection.prototype.get = function (key, callback) {
return callback(new Error('Connection not started'));
}

var segment = this.cache[key.segment];
const segment = this.cache[key.segment];

if (!segment) {
return callback(null, null);
}

var envelope = segment[key.id];
const envelope = segment[key.id];

if (!envelope) {
return callback(null, null);
}

var value = null;
let value = null;

if (Buffer.isBuffer(envelope.item)) {
value = envelope.item;
Expand All @@ -139,7 +143,7 @@ internals.Connection.prototype.get = function (key, callback) {
}
}

var result = {
const result = {
item: value,
stored: envelope.stored,
ttl: envelope.ttl
Expand All @@ -151,8 +155,6 @@ internals.Connection.prototype.get = function (key, callback) {

internals.Connection.prototype.set = function (key, value, ttl, callback) {

var self = this;

callback = Hoek.nextTick(callback);

if (!this.cache) {
Expand All @@ -163,33 +165,32 @@ internals.Connection.prototype.set = function (key, value, ttl, callback) {
return callback(new Error('Invalid ttl (greater than 2147483647)'));
}

var envelope = null;
let envelope = null;
try {
envelope = new internals.MemoryCacheEntry(key, value, ttl, this.settings.allowMixedContent);
} catch (err) {
}
catch (err) {
return callback(err);
}

this.cache[key.segment] = this.cache[key.segment] || new internals.MemoryCacheSegment();
var segment = this.cache[key.segment];

var cachedItem = segment[key.id];
if (cachedItem &&
cachedItem.timeoutId) {
const segment = this.cache[key.segment];
const cachedItem = segment[key.id];

if (cachedItem && cachedItem.timeoutId) {
clearTimeout(cachedItem.timeoutId);
self.byteSize -= cachedItem.byteSize; // If the item existed, decrement the byteSize as the value could be different
this.byteSize -= cachedItem.byteSize; // If the item existed, decrement the byteSize as the value could be different
}

if (this.settings.maxByteSize) {
if (self.byteSize + envelope.byteSize > this.settings.maxByteSize) {
if (this.byteSize + envelope.byteSize > this.settings.maxByteSize) {
return callback(new Error('Cache size limit reached'));
}
}

var timeoutId = setTimeout(function () {
const timeoutId = setTimeout(() => {

self.drop(key, function () { });
this.drop(key, () => {});
}, ttl);

envelope.timeoutId = timeoutId;
Expand All @@ -209,9 +210,9 @@ internals.Connection.prototype.drop = function (key, callback) {
return callback(new Error('Connection not started'));
}

var segment = this.cache[key.segment];
const segment = this.cache[key.segment];
if (segment) {
var item = segment[key.id];
const item = segment[key.id];

if (item) {
this.byteSize -= item.byteSize;
Expand All @@ -226,7 +227,7 @@ internals.Connection.prototype.drop = function (key, callback) {

internals.parseJSON = function (json) {

var obj = null;
let obj = null;

try {
obj = JSON.parse(json);
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "catbox-memory",
"description": "Memory adapter for catbox",
"version": "1.1.2",
"version": "2.0.0",
"repository": "git://github.com/hapijs/catbox-memory",
"main": "lib/index.js",
"keywords": [
Expand All @@ -10,15 +10,15 @@
"memory"
],
"engines": {
"node": ">=0.10.40"
"node": ">=4.0.0"
},
"dependencies": {
"hoek": "2.x.x"
"hoek": "3.x.x"
},
"devDependencies": {
"catbox": "6.x.x",
"lab": "5.x.x",
"code": "1.x.x"
"lab": "7.x.x",
"code": "2.x.x"
},
"scripts": {
"test": "lab -a code -v -L -t 100",
Expand Down
Loading

0 comments on commit 0dbbd16

Please sign in to comment.