Skip to content

Commit

Permalink
Added logging
Browse files Browse the repository at this point in the history
  • Loading branch information
mallocator committed Mar 22, 2017
1 parent bb05294 commit f0aaccb
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 27 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
cache/
node_modules/
logs/
*.log
*.tmp
coverage/
42 changes: 34 additions & 8 deletions cache.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const Logging = require('cheevr-logging');

/**
* A payload object, that can be anything that can be stored by the cache implementation.
* @typedef {*} Payload
Expand All @@ -21,7 +23,9 @@ class Cache {
this._name = name;
this._type = config.type;
this._ttl = features.ttl && config.ttl;
this._log = Logging[config.logger];
this._timeouts = {};
this._log.info('Set up cache with name %s of type %s', this.name, this.type);
}

/**
Expand Down Expand Up @@ -57,8 +61,12 @@ class Cache {
this._timeouts[type][id] && clearTimeout(this._timeouts[type][id]);
this._timeouts[type][id] = setTimeout(this.remove.bind(this), this._ttl, type, id);
}
this._log.trace('Storing payload under key [%s:%s] in %s cache (%s)', type, id, this.name, this.type);
if (cb) {
return this._store(type, id, payload).then(result => cb(null, result)).catch(cb)
return this._store(type, id, payload).then(result => cb(null, result)).catch(err => {
this._log.error('Unable to store payload for key [%s:%s] in cache of type %s:', type, id, this.type, err);
cb(err);
})
} else {
return this._store(type, id, payload);
}
Expand All @@ -79,8 +87,12 @@ class Cache {
this._timeouts[type][id] && clearTimeout(this._timeouts[type][id]);
this._timeouts[type][id] = setTimeout(this.remove.bind(this, type, id), this._ttl);
}
this._log.trace('Fetching payload for key [%s:%s] from %s cache (%s)', type, id, this.name, this.type);
if (cb) {
return this._fetch(type, id).then(result => cb(null, result)).catch(cb);
return this._fetch(type, id).then(result => cb(null, result)).catch(err => {
this._log.error('Unable to fetch payload for key [%s:%s] from cache of type %s:', type, id, this.type, err);
cb(err);
});
} else {
return this._fetch(type, id);
}
Expand All @@ -96,7 +108,10 @@ class Cache {
list(type, cb) {
type = typeof type === 'string' ? type : String(type);
if (cb) {
this._list(type).then(result => cb(null, result)).catch(cb);
this._list(type).then(result => cb(null, result)).catch(err => {
this._log.error('Unable to list payloads for key [%s] from cache of type %s', type, this.type, err);
cb(err);
});
} else {
return this._list(type);
}
Expand All @@ -112,7 +127,10 @@ class Cache {
map(type, cb) {
type = typeof type === 'string' ? type : String(type);
if (cb) {
this._map(type).then(result => cb(null, result)).catch(cb);
this._map(type).then(result => cb(null, result)).catch(err => {
this._log.error('Unable create map for key [%s] from cache of type %s', type, this.type, err);
cb(err);
});
} else {
return this._map(type);
}
Expand All @@ -133,8 +151,12 @@ class Cache {
this._timeouts[type][id] && clearTimeout(this._timeouts[type][id]);
delete this._timeouts[type][id];
}
this._log.trace('Removing payload for key [%s:%s] from %s cache (%s)', type, id, this.name, this.type);
if (cb) {
this._remove(type, id).then(result => cb(null, result)).catch(cb);
this._remove(type, id).then(result => cb(null, result)).catch(err => {
this._log.error('Unable to remove entry for key [%s:%s] form cache of type %s:', type, id, this.type, err);
cb(err);
});
} else {
return this._remove(type, id);
}
Expand All @@ -143,8 +165,8 @@ class Cache {
/**
* Removes all payload of a given type from cache. Supports both callback and promise interface. Will
* remove the ttl of all entries affected.
* @param type
* @param cb
* @param {string} type
* @param {Callback} cb
* @returns {*}
*/
clear(type, cb) {
Expand All @@ -153,8 +175,12 @@ class Cache {
this._timeouts[type].forEach(clearTimeout);
delete this._timeouts[type];
}
this._log.trace('Clearing cache for type [%s] from %s cache (%s)', type, this.name, this.type);
if (cb) {
this._clear(type).then(result => cb(null, result)).catch(cb);
this._clear(type).then(result => cb(null, result)).catch(err => {
this._log.error('Unable to clear entries for key [%s] from cache of type %s:', type, this.type, err);
cb(err);
});
} else {
return this._clear(type);
}
Expand Down
1 change: 1 addition & 0 deletions config/defaults.cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = {
logger: 'cache'
},
redis: {
logger: 'cache',
host: 'localhost',
port: 6379,
prefix: '',
Expand Down
5 changes: 5 additions & 0 deletions config/logging.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
loggers: {
cache: 'warn'
}
};
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Manager {
name = '_default_';
}
instanceConfig = instanceConfig || this._config[name] || { type: this._defaults.defaultType };
_.defaultsDeep(instanceConfig, this._defaults[config.type]);
_.defaultsDeep(instanceConfig, this._defaults[instanceConfig.type]);
instanceConfig.type = instanceConfig.type.toLowerCase().trim();
return new (require('./' + instanceConfig.type))(instanceConfig, name);
}
Expand Down
13 changes: 11 additions & 2 deletions test/cache.test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
/* global describe, it, after, before, afterEach, beforeEach */
const expect = require('chai').expect;
const Logging = require('cheevr-logging');

const Memory = require('../memory');


Logging.cache = {
trace: () => {},
debug: () => {},
info: () => {},
warn: () => {},
error: () => {}
};

describe('Cache', () => {
async function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

it('should expire an entry after a given time', async () => {
let memory = new Memory({type: 'memory', ttl: 50}, 'Test');
let memory = new Memory({type: 'memory', logger:'cache', ttl: 50}, 'Test');

await memory.store('TestType', 1, {a: 'Test'});
let result1 = await memory.fetch('TestType', 1);
Expand All @@ -22,7 +31,7 @@ describe('Cache', () => {
});

it('should reset the ttl if stored or fetched again', async () => {
let memory = new Memory({type: 'memory', ttl: 50}, 'Test');
let memory = new Memory({type: 'memory', logger:'cache', ttl: 50}, 'Test');

await memory.store('TestType', 1, {a: 'Test'});
let result1 = await memory.fetch('TestType', 1);
Expand Down
17 changes: 13 additions & 4 deletions test/file.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
/* global describe, it, after, before, afterEach, beforeEach */
const expect = require('chai').expect;
const fs = require('fs');
const Logging = require('cheevr-logging');
const path = require('path');

const File = require('../file');


Logging.cache = {
trace: () => {},
debug: () => {},
info: () => {},
warn: () => {},
error: () => {}
};

describe('File', () => {
it('should create a file cache instance', () => {
let file = new File({type: 'file'}, 'Test');
let file = new File({type: 'file', logger: 'cache'}, 'Test');
expect(file.type).to.equal('file');
expect(file.name).to.equal('Test');
expect(file).to.respondTo('_store');
Expand All @@ -20,7 +29,7 @@ describe('File', () => {
});

it('should store and retrieve a cache value using callbacks', done => {
let file = new File({type: 'file'}, 'Test');
let file = new File({type: 'file', logger: 'cache'}, 'Test');
file.store('TestType', 1, {a: 'Test'}, () => {
file.fetch('TestType', 1, (err, result) => {
expect(result).to.deep.equal({a: 'Test'});
Expand All @@ -30,14 +39,14 @@ describe('File', () => {
});

it('should store and retrieve a cache value using promises', async () => {
let file = new File({type: 'file'}, 'Test');
let file = new File({type: 'file', logger: 'cache'}, 'Test');
await file.store('TestType', 1, {a: 'Test'});
let result = await file.fetch('TestType', 1);
expect(result).to.deep.equal({a: 'Test'});
});

it('should support all standard operation for caching', async () => {
let file = new File({type: 'file'}, 'Test');
let file = new File({type: 'file', logger: 'cache'}, 'Test');
await file.store('TestType', 1, {a: 'Test1'});
await file.store('TestType', 2, {a: 'Test2'});

Expand Down
13 changes: 10 additions & 3 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
/* global describe, it, after, before, afterEach, beforeEach */
const expect = require('chai').expect;
const Logging = require('cheevr-logging');

const Cache = require('..');


process.on('unhandledRejection', console.error)
Logging.cache = {
trace: () => {},
debug: () => {},
info: () => {},
warn: () => {},
error: () => {}
};

describe('Manager', () => {
beforeEach(() => Cache.reset());
Expand All @@ -14,12 +21,12 @@ describe('Manager', () => {
});

it('should return the named cache instance', () => {
Cache.configure({ myfiles: { type: 'file' }});
Cache.configure({ myfiles: { type: 'file', logger: 'cache' }});
expect(Cache.instance('myfiles').type).to.equal('file');
});

it('should by default call the instance _default_', () => {
let instance = Cache.instance({type: 'file'});
let instance = Cache.instance({type: 'file', logger: 'cache'});
expect(instance.name).to.equal('_default_');
expect(instance.type).to.equal('file');
})
Expand Down
17 changes: 13 additions & 4 deletions test/memory.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
/* global describe, it, after, before, afterEach, beforeEach */
const expect = require('chai').expect;
const Logging = require('cheevr-logging');

const Memory = require('../memory');


Logging.cache = {
trace: () => {},
debug: () => {},
info: () => {},
warn: () => {},
error: () => {}
};

describe('Memory', () => {
it('should create a memory cache instance', () => {
let memory = new Memory({type: 'memory'}, 'Test');
let memory = new Memory({type: 'memory', logger: 'cache'}, 'Test');
expect(memory.type).to.equal('memory');
expect(memory.name).to.equal('Test');
expect(memory).to.respondTo('_store');
Expand All @@ -18,7 +27,7 @@ describe('Memory', () => {
});

it('should store and retrieve a cache value using callbacks', done => {
let memory = new Memory({type: 'memory'}, 'Test');
let memory = new Memory({type: 'memory', logger: 'cache'}, 'Test');
memory.store('TestType', 1, {a: 'Test'}, () => {
memory.fetch('TestType', 1, (err, result) => {
expect(result).to.deep.equal({a: 'Test'});
Expand All @@ -28,14 +37,14 @@ describe('Memory', () => {
});

it('should store and retrieve a cache value using promises', async () => {
let memory = new Memory({type: 'memory'}, 'Test');
let memory = new Memory({type: 'memory', logger: 'cache'}, 'Test');
await memory.store('TestType', 1, {a: 'Test'});
let result = await memory.fetch('TestType', 1);
expect(result).to.deep.equal({a: 'Test'});
});

it('should support all standard operation for caching', async () => {
let memory = new Memory({type: 'memory'}, 'Test');
let memory = new Memory({type: 'memory', logger: 'cache'}, 'Test');
await memory.store('TestType', 1, {a: 'Test1'});
await memory.store('TestType', 2, {a: 'Test2'});

Expand Down
19 changes: 14 additions & 5 deletions test/redis.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@
try{ require('redis'); } catch(e) { return; }
const expect = require('chai').expect;
const fs = require('fs');
const Logging = require('cheevr-logging');
const path = require('path');

const Redis = require('../redis');


Logging.cache = {
trace: () => {},
debug: () => {},
info: () => {},
warn: () => {},
error: () => {}
};

describe('Redis', () => {
it('should create a redis cache instance', () => {
let redis = new Redis({type: 'redis'}, 'Test');
let redis = new Redis({type: 'redis', logger: 'cache'}, 'Test');
expect(redis.type).to.equal('redis');
expect(redis.name).to.equal('Test');
expect(redis).to.respondTo('_store');
Expand All @@ -21,7 +30,7 @@ describe('Redis', () => {
});

it('should store and retrieve a cache value using callbacks', done => {
let redis = new Redis({type: 'redis'}, 'Test');
let redis = new Redis({type: 'redis', logger: 'cache'}, 'Test');
redis.store('TestType', 1, {a: 'Test'}, () => {
redis.fetch('TestType', 1, (err, result) => {
expect(result).to.deep.equal({a: 'Test'});
Expand All @@ -32,15 +41,15 @@ describe('Redis', () => {
});

it('should store and retrieve a cache value using promises', async () => {
let redis = new Redis({type: 'redis'}, 'Test');
let redis = new Redis({type: 'redis', logger: 'cache'}, 'Test');
await redis.store('TestType', 1, {a: 'Test'});
let result = await redis.fetch('TestType', 1);
expect(result).to.deep.equal({a: 'Test'});
await redis.clear('TestType');
});

it('should support all standard operation for caching without ttl', async () => {
let redis = new Redis({type: 'redis'}, 'Test');
let redis = new Redis({type: 'redis', logger: 'cache'}, 'Test');
await redis.store('TestType', 1, {a: 'Test1'});
await redis.store('TestType', 2, {a: 'Test2'});

Expand All @@ -61,7 +70,7 @@ describe('Redis', () => {
});

it('should support all standard operation for caching with a ttl', async () => {
let redis = new Redis({type: 'redis', ttl: 10000 }, 'Test');
let redis = new Redis({type: 'redis', logger: 'cache', ttl: 10000 }, 'Test');
await redis.store('TestType', 1, {a: 'Test1'});
await redis.store('TestType', 2, {a: 'Test2'});

Expand Down

0 comments on commit f0aaccb

Please sign in to comment.