New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

cache-service

Package Overview
Dependencies
Maintainers
1
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cache-service - npm Package Compare versions

Comparing version 0.2.5 to 0.2.6

2

modules/cacheModules/nodeCacheModule.js

@@ -55,3 +55,3 @@ var cacheModule = require('./cacheModule');

if(obj.hasOwnProperty(key)){
var tempExpiration = expiration;
var tempExpiration = expiration || this.expiration;
var value = obj[key];

@@ -58,0 +58,0 @@ if(typeof value === 'object' && value.cacheValue){

@@ -113,3 +113,3 @@ var cacheModule = require('./cacheModule');

if(obj.hasOwnProperty(key)){
var tempExpiration = expiration;
var tempExpiration = expiration || this.expiration;
var value = obj[key];

@@ -125,7 +125,7 @@ if(typeof value === 'object' && value.cacheValue){

}
multi.setex(key, tempExpiration, value, noop);
multi.setex(key, tempExpiration, value);
}
}
multi.exec(function (err, replies){
if(cb) cb();
if(cb) cb(err, replies);
});

@@ -132,0 +132,0 @@ }

{
"name": "cache-service",
"version": "0.2.5",
"version": "0.2.6",
"description": "A tiered caching solution for node.",

@@ -13,3 +13,3 @@ "main": "index.js",

"expect": "1.6.0",
"redis-mock": "0.4.8"
"mock-redis-client": "0.90.21"
},

@@ -16,0 +16,0 @@ "scripts": {

@@ -9,3 +9,3 @@ # cache-service

cache-service allows you to create redundant, cache-agnostic caching configurations. By default, it supports redis and node-cach, but you can add any cache you want as long as you follow the same interface.
cache-service allows you to create redundant, cache-agnostic caching configurations. By default, it supports [redis](http://redis.io/) (using [node_redis](https://github.com/mranney/node_redis)) and [node-cache](https://github.com/tcs-de/nodecache), but you can add any cache you want as long as you follow the [same interface](#cache-module-interface).

@@ -238,6 +238,65 @@ # Basic Usage

# Standalone Cache Module Usage
When you pass a `cacheModuleConfig` to cache-service's constructor, it internally instantiates cache modules based on what data you provide. For example, the following `cacheModuleConfig` will internally instantiate a single nodeCacheModule instance with the given settings:
```javascript
[
{
type: 'node-cache',
defaultExpiration: 60,
cacheWhenEmpty: false
}
]
```
But what if you want to manually instantiate your cache modules? There are several reason you might want to do this including:
* Having more testable code
* Knowing that a redis connection was successful before attempting to create a cache-service instance
* Having an external reference to a cache module without having to drill into cache-service's innards
* Injecting a custom cache module of your own creation (for example a mem-cache or a mongo cache module)
* Simply wanting to use a cache module and not cache-service (perhaps you like the extra convenience features like being able to add expirations to `.mset()` or built-in logging)
## Require, Instantiate, and Inject
#### Require
cache-service provides two native cache modules. A cache module is simply a wrapper for a cache type. The modules provided are for node-cache and redis. To require nodeCacheModule for manual instantiation, do the following:
```javascript
var nodeCacheModule = require('cache-service').nodeCacheModule;
```
#### Instantiate
To instantiate it, simply pass almost the same object we passed above in the `cacheModuleConfig` array as follows:
```javascript
var nodeCacheModuleInstance = new nodeCacheModule({
//type is not necessary since we're instantiating a specific type manually
defaultExpiration: 60,
cacheWhenEmpty: false
}).cache;
```
#### Inject
Now let's pass our manually instantiated nodeCacheModuleInstance into our cache-service constructor:
```javascript
var cacheService = new cs({}, [
{
type: 'custom', //A type of 'custom' tells cache-service that this cache has already been instantiated
cache: nodeCacheModuleInstance
}
]);
```
# Cache Module Interface
Documentation coming soon.
# Roadmap
* Add standalone cache usage documentation and examples
* Add `.mget()` and `.mset()` functions
* Upgrade from redis-mock to mock-redis-client
* ~~Add standalone cache usage documentation and examples~~
* ~~Add `.mget()` and `.mset()` functions~~
* ~~Upgrade from redis-mock to mock-redis-client~~ (My PRs were merged for it and its dependency.)
* Look into upgrading from mock-redis-client to redis-js or fakeredis (I submitted a PR to redis-js and an issue to fakeredis. Will do more when one of them is merged/fixed.)
* Add cache module interface documentation and examples

@@ -0,4 +1,11 @@

// Possible better redis mocks to use in the future
// "redis-js": "0.0.12-3"
// var redisMock = require('redis-js');
// "fakeredis": "0.3.1"
// var redisMock = require("fakeredis").createClient();
var expect = require('expect');
var cs = require('../../modules/cacheService');
var rMock = require('redis-mock');
var rMock = require('mock-redis-client').createMockRedis();
var rcModule = require('../../modules/cacheModules/redisCacheModule');

@@ -14,34 +21,74 @@ var redisMock = rMock.createClient();

describe('Array', function(){
var key = 'key';
var value = 'value';
var key = 'key';
var value = 'value';
beforeEach(function(){
cacheService.flush();
});
beforeEach(function(){
cacheService.flush();
});
describe('cachService API tests', function () {
it('.set(k, v), .get(k)', function (done) {
cacheService.set(key, value);
describe('cachService API tests', function () {
it('.set(k, v), .get(k)', function (done) {
cacheService.set(key, value);
cacheService.get(key, function (err, response){
expect(response).toBe('value');
done();
});
});
it('.set(k, v, exp), .get(k)', function (done) {
this.timeout(5000);
cacheService.set(key, value, 1);
setTimeout(function(){
cacheService.get(key, function (err, response){
expect(response).toBe('value');
expect(response).toBe(null);
done();
});
}, 2100);
});
it('.del(string)', function (done) {
cacheService.set(key, value);
cacheService.del(key, function (err, count){
expect(count).toBe(1);
cacheService.get(key, function (err, response){
expect(response).toBe(null);
done();
});
});
it('.set(k, v, exp), .get(k)', function (done) {
cacheService.set(key, value, 0.001);
setTimeout(function(){
cacheService.get(key, function (err, response){
});
it('.del(array)', function (done) {
cacheService.set(key, value);
cacheService.set('key2', 'value2');
cacheService.del([key, 'key2'], function (err, count){
expect(count).toBe(2);
cacheService.get(key, function (err, response){
expect(response).toBe(null);
cacheService.get('key2', function (err, response){
expect(response).toBe(null);
done();
});
}, 10);
});
});
it('.del(string)', function (done) {
cacheService.set(key, value);
cacheService.del(key, function (err, count){
expect(count).toBe(1);
});
});
describe('cachService caching tests', function () {
it('.set() should add data to all caches', function (done) {
cacheService.set(key, value);
var caches = cacheService.cacheCollection.preApi;
for(var i = 0; i < caches.length; i++){
var curCache = caches[i];
curCache.get(key, function (err, response){
expect(response).toBe(value);
});
}
done();
});
it('.get(k) should search subsequent caches with longer default expirations when k is not found in earlier caches', function (done) {
cacheService.set(key, value);
var firstCache = cacheService.cacheCollection.preApi[0];
firstCache.del(key, function (err, count){
expect(count).toBe(1);
firstCache.get(key, function (err, response){
expect(response).toBe(null);
cacheService.get(key, function (err, response){
expect(response).toBe(null);
expect(response).toBe(value);
done();

@@ -51,152 +98,108 @@ });

});
it('.del(array)', function (done) {
cacheService.set(key, value);
cacheService.set('key2', 'value2');
cacheService.del([key, 'key2'], function (err, count){
expect(count).toBe(2);
cacheService.get(key, function (err, response){
expect(response).toBe(null);
cacheService.get('key2', function (err, response){
expect(response).toBe(null);
done();
});
});
});
});
});
describe('cachService caching tests', function () {
it('.set() should add data to all caches', function (done) {
cacheService.set(key, value);
var caches = cacheService.cacheCollection.preApi;
for(var i = 0; i < caches.length; i++){
var curCache = caches[i];
curCache.get(key, function (err, response){
expect(response).toBe(value);
});
}
});
it('.mget() should return all available keys (exact key number match)', function (done) {
cacheService.set(key, value);
cacheService.set('key2', 'value2');
cacheService.set('key3', 'value3');
cacheService.mget([key, 'key2', 'key3'], function (err, response){
expect(response.key).toBe('value');
expect(response.key2).toBe('value2');
expect(response.key3).toBe('value3');
done();
})
});
it('.mget() should return all available keys (not an exact key number match)', function (done) {
cacheService.set(key, value);
cacheService.set('key2', 'value2');
cacheService.set('key3', 'value3');
cacheService.mget([key, 'key2', 'key3', 'key4'], function (err, response){
expect(response.key).toBe('value');
expect(response.key2).toBe('value2');
expect(response.key3).toBe('value3');
expect(response.key4).toBe(undefined);
done();
})
});
it('Setting several keys via .mset() then calling .mget() should retrieve all keys (exact key number match)', function (done) {
cacheService.mset({key: value, 'key2': 'value2', 'key3': 'value3'});
cacheService.mget([key, 'key2', 'key3'], function (err, response){
expect(response.key).toBe('value');
expect(response.key2).toBe('value2');
expect(response.key3).toBe('value3');
done();
})
});
it('Setting several keys via .mset() then calling .mget() should retrieve all keys (not an exact key number match)', function (done) {
cacheService.mset({key: value, 'key2': 'value2', 'key3': 'value3'});
cacheService.mget([key, 'key2', 'key3', 'key4'], function (err, response){
expect(response.key).toBe('value');
expect(response.key2).toBe('value2');
expect(response.key3).toBe('value3');
expect(response.key4).toBe(undefined);
done();
});
it('.get(k) should search subsequent caches with longer default expirations when k is not found in earlier caches', function (done) {
cacheService.set(key, value);
var firstCache = cacheService.cacheCollection.preApi[0];
firstCache.del(key, function (err, count){
expect(count).toBe(1);
firstCache.get(key, function (err, response){
expect(response).toBe(null);
cacheService.get(key, function (err, response){
expect(response).toBe(value);
done();
});
});
});
});
it('.mget() should return all available keys (exact key number match)', function (done) {
cacheService.set(key, value);
cacheService.set('key2', 'value2');
cacheService.set('key3', 'value3');
cacheService.mget([key, 'key2', 'key3'], function (err, response){
expect(response.key).toBe('value');
expect(response.key2).toBe('value2');
expect(response.key3).toBe('value3');
done();
})
});
it('.mget() should return all available keys (not an exact key number match)', function (done) {
cacheService.set(key, value);
cacheService.set('key2', 'value2');
cacheService.set('key3', 'value3');
cacheService.mget([key, 'key2', 'key3', 'key4'], function (err, response){
expect(response.key).toBe('value');
expect(response.key2).toBe('value2');
expect(response.key3).toBe('value3');
expect(response.key4).toBe(undefined);
done();
})
});
//redis-mock does not yet support .mset()
// it('Setting several keys via .mset() then calling .mget() should retrieve all keys (exact key number match)', function (done) {
// cacheService.mset({key: value, 'key2': 'value2', 'key3': 'value3'});
// cacheService.mget([key, 'key2', 'key3'], function (err, response){
// expect(response.key).toBe('value');
// expect(response.key2).toBe('value2');
// expect(response.key3).toBe('value3');
// done();
// })
// });
// it('Setting several keys via .mset() then calling .mget() should retrieve all keys (not an exact key number match)', function (done) {
// cacheService.mset({key: value, 'key2': 'value2', 'key3': 'value3'});
// cacheService.mget([key, 'key2', 'key3', 'key4'], function (err, response){
// expect(response.key).toBe('value');
// expect(response.key2).toBe('value2');
// expect(response.key3).toBe('value3');
// expect(response.key4).toBe(undefined);
// done();
// })
// });
});
});
describe('cachService performance tests (50ms added to all tests)', function () {
var speedTest = new cs({}, [
{type: 'node-cache'},
{type: 'node-cache', defaultExpiration: 1600}
]);
describe('cachService performance tests (50ms added to all tests)', function () {
var speedTest = new cs({}, [
{type: 'node-cache'},
{type: 'node-cache', defaultExpiration: 1600}
]);
var list = {};
var list2 = {};
var list3 = [];
var ITERATIONS = 50;
var list = {};
var list2 = {};
var list3 = [];
var ITERATIONS = 50;
for(var i = 0; i < ITERATIONS; ++i){
list['key' + i] = 'value' + i;
list2['key' + i + i] = 'value' + i + i;
list3.push('key' + i + i);
}
beforeEach(function(){
speedTest.flush();
speedTest.mset(list2);
});
it('.set() x 50', function (done) {
for(var i = 0; i < ITERATIONS; ++i){
list['key' + i] = 'value' + i;
list2['key' + i + i] = 'value' + i + i;
list3.push('key' + i + i);
speedTest.set('key' + i, 'value' + i, null, function(){
if(i == ITERATIONS - 1){
setTimeout(function(){
done();
}, 50);
}
});
}
});
beforeEach(function(){
speedTest.flush();
speedTest.mset(list2);
it('.mset() x 50', function (done) {
speedTest.mset(list, null, function(){
setTimeout(function(){
done();
}, 50);
});
});
it('.set() x 50', function (done) {
for(var i = 0; i < ITERATIONS; ++i){
speedTest.set('key' + i, 'value' + i, null, function(){
if(i == ITERATIONS - 1){
setTimeout(function(){
done();
}, 50);
}
});
}
});
it('.mset() x 50', function (done) {
speedTest.mset(list, null, function(){
setTimeout(function(){
done();
}, 50);
it('.get() x 50', function (done) {
for(var i = 0; i < ITERATIONS; ++i){
speedTest.get('key' + i + i, function(){
if(i == ITERATIONS - 1){
setTimeout(function(){
done();
}, 50);
}
});
});
}
});
it('.get() x 50', function (done) {
for(var i = 0; i < ITERATIONS; ++i){
speedTest.get('key' + i + i, function(){
if(i == ITERATIONS - 1){
setTimeout(function(){
done();
}, 50);
}
});
}
it('.mget() x 50', function (done) {
speedTest.mget(list3, function(){
setTimeout(function(){
done();
}, 50);
});
it('.mget() x 50', function (done) {
speedTest.mget(list3, function(){
setTimeout(function(){
done();
}, 50);
});
});
});
});

@@ -5,66 +5,64 @@ var expect = require('expect');

describe('Array', function(){
var key = 'key';
var value = 'value';
var key = 'key';
var value = 'value';
beforeEach(function(){
beforeEach(function(){
nodeCache.flushAll();
});
describe('nodeCacheModule Tests', function () {
it('Getting absent key should return null', function (done) {
nodeCache.get(key, function (err, result){
expect(result).toBe(null);
done();
});
});
it('Setting then getting key should return value', function (done) {
nodeCache.set(key, value);
nodeCache.get(key, function (err, result) {
expect(result).toBe(value);
done();
});
});
it('Setting then deleting then getting key should return null', function (done) {
nodeCache.set(key, value);
nodeCache.del(key);
nodeCache.get(key, function (err, result) {
expect(result).toBe(null);
done();
});
});
it('Setting several keys then calling .flushAll() should remove all keys', function (done) {
nodeCache.set(key, value);
nodeCache.set('key2', 'value2');
nodeCache.set('key3', 'value3');
var keyCount = nodeCache.db.getStats().keys;
expect(keyCount).toBe(3);
nodeCache.flushAll();
});
describe('nodeCacheModule Tests', function () {
it('Getting absent key should return null', function (done) {
nodeCache.get(key, function (err, result){
expect(result).toBe(null);
done();
});
var keyCount = nodeCache.db.getStats().keys;
expect(keyCount).toBe(0);
done();
});
it('Setting several keys then calling .mget() should retrieve all keys', function (done) {
nodeCache.set(key, value);
nodeCache.set('key2', 'value2');
nodeCache.set('key3', 'value3');
nodeCache.mget([key, 'key2', 'key3', 'key4'], function (err, response){
expect(response.key).toBe('value');
expect(response.key2).toBe('value2');
expect(response.key3).toBe('value3');
expect(response.key4).toBe(undefined);
done();
});
it('Setting then getting key should return value', function (done) {
nodeCache.set(key, value);
nodeCache.get(key, function (err, result) {
expect(result).toBe(value);
done();
});
});
it('Setting several keys via .mset() then calling .mget() should retrieve all keys', function (done) {
nodeCache.mset({key: value, 'key2': 'value2', 'key3': 'value3'});
nodeCache.mget([key, 'key2', 'key3', 'key4'], function (err, response){
expect(response.key).toBe('value');
expect(response.key2).toBe('value2');
expect(response.key3).toBe('value3');
expect(response.key4).toBe(undefined);
done();
});
it('Setting then deleting then getting key should return null', function (done) {
nodeCache.set(key, value);
nodeCache.del(key);
nodeCache.get(key, function (err, result) {
expect(result).toBe(null);
done();
});
});
it('Setting several keys then calling .flushAll() should remove all keys', function (done) {
nodeCache.set(key, value);
nodeCache.set('key2', 'value2');
nodeCache.set('key3', 'value3');
var keyCount = nodeCache.db.getStats().keys;
expect(keyCount).toBe(3);
nodeCache.flushAll();
var keyCount = nodeCache.db.getStats().keys;
expect(keyCount).toBe(0);
done();
});
it('Setting several keys then calling .mget() should retrieve all keys', function (done) {
nodeCache.set(key, value);
nodeCache.set('key2', 'value2');
nodeCache.set('key3', 'value3');
nodeCache.mget([key, 'key2', 'key3', 'key4'], function (err, response){
expect(response.key).toBe('value');
expect(response.key2).toBe('value2');
expect(response.key3).toBe('value3');
expect(response.key4).toBe(undefined);
done();
});
});
it('Setting several keys via .mset() then calling .mget() should retrieve all keys', function (done) {
nodeCache.mset({key: value, 'key2': 'value2', 'key3': 'value3'});
nodeCache.mget([key, 'key2', 'key3', 'key4'], function (err, response){
expect(response.key).toBe('value');
expect(response.key2).toBe('value2');
expect(response.key3).toBe('value3');
expect(response.key4).toBe(undefined);
done();
});
});
});
});
});
var expect = require('expect');
var rMock = require('redis-mock');
var rMock = require('mock-redis-client').createMockRedis();
var rcModule = require('../../modules/cacheModules/redisCacheModule');

@@ -7,51 +7,60 @@ var redisMock = rMock.createClient();

describe('Array', function(){
var key = 'key';
var value = 'value';
var key = 'key';
var value = 'value';
beforeEach(function(){
redisCache.flushAll();
});
beforeEach(function(){
redisCache.flushAll();
});
describe('redisCacheModule Tests', function () {
it('Getting absent key should return null', function (done) {
redisCache.get(key, function (err, result){
expect(result).toBe(null);
done();
});
describe('redisCacheModule Tests', function () {
it('Getting absent key should return null', function (done) {
redisCache.get(key, function (err, result){
expect(result).toBe(null);
done();
});
});
it('Setting then getting key should return value', function (done) {
redisCache.set(key, value);
redisCache.get(key, function (err, result) {
expect(result).toBe(value);
done();
});
it('Setting then getting key should return value', function (done) {
redisCache.set(key, value);
redisCache.get(key, function (err, result) {
expect(result).toBe(value);
done();
});
});
it('Setting then deleting then getting key should return null', function (done) {
redisCache.set(key, value);
redisCache.del(key);
redisCache.get(key, function (err, result) {
expect(result).toBe(null);
done();
});
it('Setting then deleting then getting key should return null', function (done) {
redisCache.set(key, value);
redisCache.del(key);
redisCache.get(key, function (err, result) {
expect(result).toBe(null);
});
it('Setting several keys then calling .flushAll() should remove all keys', function (done) {
redisCache.set(key, value);
redisCache.set('key2', 'value2');
redisCache.set('key3', 'value3');
redisCache.db.keys('*', function (err, keys){
var keyCount = keys.length;
expect(keyCount).toBe(3);
redisCache.flushAll();
redisCache.db.keys('*', function (err, keys){
keyCount = keys.length;
expect(keyCount).toBe(0);
done();
});
});
it('Setting several keys then calling .flushAll() should remove all keys', function (done) {
redisCache.set(key, value);
redisCache.set('key2', 'value2');
redisCache.set('key3', 'value3');
redisCache.db.keys('*', function (err, keys){
var keyCount = keys.length;
expect(keyCount).toBe(3);
redisCache.flushAll();
redisCache.db.keys('*', function (err, keys){
keyCount = keys.length;
expect(keyCount).toBe(0);
done();
});
});
});
it('Setting several keys then calling .mget() should retrieve all keys', function (done) {
redisCache.set(key, value);
redisCache.set('key2', 'value2');
redisCache.set('key3', 'value3');
redisCache.mget([key, 'key2', 'key3', 'key4'], function (err, response){
expect(response.key).toBe('value');
expect(response.key2).toBe('value2');
expect(response.key3).toBe('value3');
expect(response.key4).toBe(undefined);
done();
});
it('Setting several keys then calling .mget() should retrieve all keys', function (done) {
redisCache.set(key, value);
redisCache.set('key2', 'value2');
redisCache.set('key3', 'value3');
});
it('Setting several keys via .mset() then calling .mget() should retrieve all keys', function (done) {
redisCache.mset({key: value, 'key2': 'value2', 'key3': 'value3'}, null, function (err, replies){
redisCache.mget([key, 'key2', 'key3', 'key4'], function (err, response){

@@ -65,14 +74,3 @@ expect(response.key).toBe('value');

});
//redis-mock does not yet support .mset()
// it('Setting several keys via .mset() then calling .mget() should retrieve all keys', function (done) {
// redisCache.mset({key: value, 'key2': 'value2', 'key3': 'value3'});
// redisCache.mget([key, 'key2', 'key3', 'key4'], function (err, response){
// expect(response.key).toBe('value');
// expect(response.key2).toBe('value2');
// expect(response.key3).toBe('value3');
// expect(response.key4).toBe(undefined);
// done();
// });
// });
});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc