Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@opuscapita/cache

Package Overview
Dependencies
Maintainers
9
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@opuscapita/cache - npm Package Compare versions

Comparing version 1.2.0 to 1.3.0

181

index.js

@@ -26,20 +26,25 @@ const extend = require('extend');

* @param {object} [config.memory] - Redis configuration.
* @param {string} [config.keyPrefix] - Global prefix for all cache keys.
* @param {number} [config.defaultExpire] - Global expires value that applies, if no expires value is passed to one of the set-methods.
* @constructor
*/
const Cache = function(config)
{
this.config = extend(true, {}, Cache.DefaultConfig, config)
const Cache = function (config) {
/**
* @member {object} cache - Cache object.
* @type {string}
*/
this.keyPrefix = '';
this.config = extend(true, {}, Cache.DefaultConfig, config);
this.setKeyPrefix(this.config.keyPrefix || '');
this.setKeyPrefix(this.config.keyPrefix || '');
if(this.config.driver)
{
var driverFile = pathjs.resolve(pathjs.join(__dirname, this.config.pluginDirectory, this.config.driver + '.js'));
var plugin = require(driverFile);
if (this.config.driver) {
const driverFile = pathjs.resolve(pathjs.join(__dirname, this.config.pluginDirectory, `${this.config.driver}.js`));
const plugin = require(driverFile);
this.use(new plugin(this.config[this.config.driver]));
// other configuration is read from this.config
// defaultExpire is used when creating a new entry (not passed to the cache plugin during initialization)
}
}
this.use(new plugin(this.config[this.config.driver]));
// other configuration is read from this.config
// defaultExpire is used when creating a new entry (not passed to the cache plugin during initialization)
}
};

@@ -50,6 +55,5 @@ /**

*/
Cache.prototype.use = function(cache)
{
this.cache = cache;
}
Cache.prototype.use = function (cache) {
this.cache = cache;
};

@@ -60,6 +64,5 @@ /**

*/
Cache.prototype.setKeyPrefix = function(prefix)
{
this.keyPrefix = prefix;
}
Cache.prototype.setKeyPrefix = function (prefix) {
this.keyPrefix = prefix;
};

@@ -74,7 +77,6 @@ /**

*/
Cache.prototype.put = function(key, value, expires)
{
return checkCacheOrThrow(this.cache).bind(this)
.then(() => this.cache.put(this.keyPrefix + key, value, expires || this.config.defaultExpire));
}
Cache.prototype.put = function (key, value, expires) {
return checkCacheOrThrow(this.cache).bind(this).
then(() => this.cache.put(this.keyPrefix + key, value, expires || this.config.defaultExpire));
};

@@ -90,22 +92,19 @@ /**

*/
Cache.prototype.putIfMissing = function(key, value, expires)
{
var preKey = this.keyPrefix + key;
var cache = this.cache;
var self = this;
Cache.prototype.putIfMissing = function (key, value, expires) {
const preKey = this.keyPrefix + key;
const cache = this.cache;
const self = this;
return new Promise((resolve) =>
{
checkCacheOrThrow(cache).then(() =>
{
return cache.exists(preKey).then((exists) =>
{
if(exists)
resolve(false);
else
resolve(cache.put(preKey, value, expires || self.config.defaultExpire))
});
});
return new Promise((resolve) => {
checkCacheOrThrow(cache).then(() => {
return cache.exists(preKey).then((exists) => {
if (exists) {
resolve(false);
} else {
resolve(cache.put(preKey, value, expires || self.config.defaultExpire));
}
});
});
}
});
};

@@ -117,6 +116,5 @@ /**

*/
Cache.prototype.exists = function(key)
{
return checkCacheOrThrow(this.cache).bind(this).then(() => this.cache.exists(this.keyPrefix + key));
}
Cache.prototype.exists = function (key) {
return checkCacheOrThrow(this.cache).bind(this).then(() => this.cache.exists(this.keyPrefix + key));
};

@@ -126,24 +124,22 @@ /**

* @param {string} key - Cache key.
* @param {number} [waitTimeout] - Optional max timeout to wait before returning a value that does not exist in the cache. If no timeout is passed, this method will always return immediately.
* @param {number} [waitTimeout] Max timeout to wait before returning a value that does not exist in the cache.
* If no timeout is passed, this method will always return immediately.
* @returns {Promise} [Promise]{@link http://bluebirdjs.com/docs/api-reference.html} value or undefined if the key did not exist.
*/
Cache.prototype.get = function(key, waitTimeout)
{
const fullKey = this.keyPrefix + key;
Cache.prototype.get = function (key, waitTimeout) {
const fullKey = this.keyPrefix + key;
if(waitTimeout)
{
return checkCacheOrThrow(this.cache).bind(this).then(() => this.cache.exists(fullKey)).then(exists =>
{
if(exists)
return this.cache.get(fullKey);
else
return new Promise(resolve => setTimeout(resolve, waitTimeout)).then(() => this.cache.get(fullKey));
});
}
else
{
return checkCacheOrThrow(this.cache).bind(this).then(() => this.cache.get(fullKey));
}
}
if (waitTimeout) {
return checkCacheOrThrow(this.cache).bind(this).then(() => this.cache.exists(fullKey)).then(exists => {
if (exists) {
return this.cache.get(fullKey);
} else {
return new Promise(resolve => setTimeout(resolve, waitTimeout)).
then(() => this.cache.get(fullKey));
}
});
} else {
return checkCacheOrThrow(this.cache).bind(this).then(() => this.cache.get(fullKey));
}
};

@@ -155,6 +151,5 @@ /**

*/
Cache.prototype.delete = function(key)
{
return checkCacheOrThrow(this.cache).bind(this).then(() => this.cache.delete(this.keyPrefix + key));
}
Cache.prototype.delete = function (key) {
return checkCacheOrThrow(this.cache).bind(this).then(() => this.cache.delete(this.keyPrefix + key));
};

@@ -165,18 +160,17 @@ /**

*/
Cache.prototype.keys = function()
{
return checkCacheOrThrow(this.cache).bind(this).then(() => {
if (this.keyPrefix) {
return this.cache.keys(this.keyPrefix)
.filter(k => k && k.startsWith(this.keyPrefix))
.map(k => k.substr(this.keyPrefix.length) || k);
} else {
return this.cache.keys();
}
});
}
Cache.prototype.keys = function () {
return checkCacheOrThrow(this.cache).bind(this).then(() => {
if (this.keyPrefix) {
return this.cache.keys(this.keyPrefix).
filter(k => k && k.startsWith(this.keyPrefix)).
map(k => k.substr(this.keyPrefix.length) || k);
} else {
return this.cache.keys();
}
});
};
Cache.prototype.on = function(event, callback) {
return checkCacheOrThrow(this.cache).bind(this).then(() => this.cache.on(event, callback));
}
Cache.prototype.on = function (event, callback) {
return checkCacheOrThrow(this.cache).bind(this).then(() => this.cache.on(event, callback));
};

@@ -195,13 +189,12 @@ /**

Cache.DefaultConfig = {
driver : 'memory',
keyPrefix : '',
defaultExpire : 600,
pluginDirectory : 'plugins',
}
driver: 'memory',
keyPrefix: '',
defaultExpire: 600,
pluginDirectory: 'plugins',
};
function checkCacheOrThrow(cache)
{
return cache ? Promise.resolve(true) : new Promise.reject(new Error('Cache was not initialized correctly.'));
function checkCacheOrThrow(cache) {
return cache ? Promise.resolve(true) : Promise.reject(new Error('Cache was not initialized correctly.'));
}
module.exports = Cache;
{
"name": "@opuscapita/cache",
"version": "1.2.0",
"version": "1.3.0",
"description": "OpusCapita cache abstraction library",

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

"dependencies": {
"@opuscapita/config": "^3.1.4",
"@opuscapita/config": "^3.1.13",
"bluebird": "^3.5.1",

@@ -42,8 +42,9 @@ "extend": "^3.0.1",

"devDependencies": {
"@types/chai": "^4.3.1",
"@opuscapita/eslint-config-opuscapita-bnapp": "^1.3.5",
"@types/chai": "^4.3.9",
"@types/mocha": "^9.1.1",
"@types/node": "^17.0.31",
"eslint": "^8.8.0",
"eslint": "^8.52.0",
"mocha": "^9.2.0",
"mocha-junit-reporter": "^1.23.3",
"mocha": "^9.2.0",
"nyc": "^15.1.0",

@@ -50,0 +51,0 @@ "rimraf": "^2.6.1"

const Promise = require('bluebird');
var DummyCache = function(config) { }
const DummyCache = function (config) { };
DummyCache.prototype.put = function(key, value, expires)
{
return Promise.resolve(true);
}
DummyCache.prototype.put = function (key, value, expires) {
return Promise.resolve(true);
};
DummyCache.prototype.get = function(key)
{
return Promise.resolve();
}
DummyCache.prototype.get = function (key) {
return Promise.resolve();
};
DummyCache.prototype.exists = function(key)
{
return Promise.resolve(false);
}
DummyCache.prototype.exists = function (key) {
return Promise.resolve(false);
};
DummyCache.prototype.delete = function(key)
{
return Promise.resolve(false);
}
DummyCache.prototype.delete = function (key) {
return Promise.resolve(false);
};
DummyCache.prototype.keys = function(prefix)
{
return Promise.resolve([ ]);
}
DummyCache.prototype.keys = function (prefix) {
return Promise.resolve([]);
};
DummyCache.DefaultConfig = {
}
};
module.exports = DummyCache;

@@ -5,49 +5,43 @@ const Promise = require('bluebird');

const MemoryCache = function(config = {})
{
this.config = extend(true, { }, MemoryCache.DefaultConfig, config);
if (config.checkPeriod) {
this.config.checkperiod = config.checkPeriod;
// Notice uppercase P....
}
const MemoryCache = function (config = {}) {
this.config = extend(true, { }, MemoryCache.DefaultConfig, config);
if (config.checkPeriod) {
this.config.checkperiod = config.checkPeriod;
// Notice uppercase P....
}
this.cache = new NodeCache(this.config);
this.cache = new NodeCache(this.config);
this.cache = Promise.promisifyAll(this.cache);
}
this.cache = Promise.promisifyAll(this.cache);
};
MemoryCache.prototype.put = function(key, value, expires)
{
return this.cache.setAsync(key, value, expires);
}
MemoryCache.prototype.put = function (key, value, expires) {
return this.cache.setAsync(key, value, expires);
};
MemoryCache.prototype.get = function(key)
{
return this.cache.getAsync(key);
}
MemoryCache.prototype.get = function (key) {
return this.cache.getAsync(key);
};
MemoryCache.prototype.exists = function(key)
{
return this.get(key).then(result => result !== undefined);
}
MemoryCache.prototype.exists = function (key) {
return this.get(key).then(result => result !== undefined);
};
MemoryCache.prototype.delete = function(key)
{
return this.cache.delAsync(key);
}
MemoryCache.prototype.delete = function (key) {
return this.cache.delAsync(key);
};
MemoryCache.prototype.keys = function()
{
return this.cache.keys();
}
MemoryCache.prototype.keys = function () {
return this.cache.keys();
};
MemoryCache.prototype.on = function(event, callback){
return this.cache.on(event, callback);
}
MemoryCache.prototype.on = function (event, callback) {
return this.cache.on(event, callback);
};
MemoryCache.DefaultConfig = {
checkPeriod: 30,
useClones: true
}
checkPeriod: 30,
useClones: true
};
module.exports = MemoryCache;

@@ -9,74 +9,63 @@ const configLib = require('@opuscapita/config');

const RedisCache = function(config)
{
this.config = extend(true, { }, RedisCache.DefaultConfig, config);
const RedisCache = function (config) {
this.config = extend(true, { }, RedisCache.DefaultConfig, config);
this.configLib = configLib.init({ host : this.config.consul.host });
this.redis = getRedisWithConsul(this.configLib, this.config.redis);
}
this.configLib = configLib.init({host: this.config.consul.host});
this.redis = getRedisWithConsul(this.configLib, this.config.redis);
};
RedisCache.prototype.put = function(key, value, expires)
{
if(expires)
{
return this.redis.then(client => client.setAsync(key, JSON.stringify(value)) && client)
.then(client => client.expireAsync(key, parseInt(expires)));
}
else
{
return this.redis.then(client => client.setAsync(key, JSON.stringify(value)));
}
}
RedisCache.prototype.put = function (key, value, expires) {
if (expires) {
return this.redis.then(client => client.setAsync(key, JSON.stringify(value)) && client).
then(client => client.expireAsync(key, parseInt(expires)));
} else {
return this.redis.then(client => client.setAsync(key, JSON.stringify(value)));
}
};
RedisCache.prototype.get = function(key)
{
return this.redis.then(client => client.getAsync(key).then(result => result && JSON.parse(result)));
}
RedisCache.prototype.get = function (key) {
return this.redis.then(client => client.getAsync(key).then(result => result && JSON.parse(result)));
};
RedisCache.prototype.exists = function(key)
{
return this.redis.then(client => client.exists(key));
}
RedisCache.prototype.exists = function (key) {
return this.redis.then(client => client.exists(key));
};
RedisCache.prototype.delete = function(key)
{
return this.redis.then(client => client.del(key));
}
RedisCache.prototype.delete = function (key) {
return this.redis.then(client => client.del(key));
};
RedisCache.prototype.keys = function(prefix)
{
return new Promise((resolve, reject) =>
{
this.redis.then(client => client.keys((prefix || '') + '*', (err, result) =>
{
if(err)
return reject(err);
RedisCache.prototype.keys = function (prefix) {
return new Promise((resolve, reject) => {
this.redis.then(client => client.keys(`${prefix || ''}*`, (err, result) => {
if (err) {
return reject(err);
}
resolve(result);
resolve(result);
})).catch(reject);
});
};
})).catch(reject);
});
}
RedisCache.DefaultConfig = {
consul : {
host : 'consul'
},
redis : {
endpointName : 'redis',
passwordKey : 'redis/password',
connection_strategy: ()=>1000,
retry_strategy: ()=>1000
}
}
consul: {
host: 'consul'
},
redis: {
endpointName: 'redis',
passwordKey: 'redis/password',
connection_strategy: () => 1000,
retry_strategy: () => 1000
}
};
function _getRedisEndpoint(endpointName, configSvc) {
if (process.env.KUBERNETES_SERVICE_DISCOVERY){
// Use kubernetes DNS discovery
// "Config" library assumes that service is hosted always on port 80 which is not correct
return {host: endpointName, port: process.env.REDIS_PORT || 6379}
}
if (process.env.KUBERNETES_SERVICE_DISCOVERY) {
// Use kubernetes DNS discovery
// "Config" library assumes that service is hosted always on port 80 which is not correct
return {host: endpointName, port: process.env.REDIS_PORT || 6379};
}
// Fetch from config service (consul)
return configSvc.getEndPoint(endpointName)
// Fetch from config service (consul)
return configSvc.getEndPoint(endpointName);
}

@@ -88,26 +77,25 @@

* Redis password is fetched using config library is config.passwordKey is defined
* @param {*} configLib
* @param {*} config
* @returns
* @param {*} configLib
* @param {*} config
* @returns
*/
async function getRedisWithConsul(configLib, config)
{
const configSvc = await configLib;
const redisEndpoint = await _getRedisEndpoint(config.endpointName, configSvc);
const options = {
host : redisEndpoint.host,
port : redisEndpoint.port,
connection_strategy : config.connection_strategy,
retry_strategy : config.retry_strategy
};
if(config.passwordKey) {
const redisPassword = await configSvc.getProperty(config.passwordKey);
if(redisPassword) {
options.password = redisPassword
}
async function getRedisWithConsul(configLib, config) {
const configSvc = await configLib;
const redisEndpoint = await _getRedisEndpoint(config.endpointName, configSvc);
const options = {
host: redisEndpoint.host,
port: redisEndpoint.port,
connection_strategy: config.connection_strategy,
retry_strategy: config.retry_strategy
};
if (config.passwordKey) {
const redisPassword = await configSvc.getProperty(config.passwordKey);
if (redisPassword) {
options.password = redisPassword;
}
}
return redis.createClient(options);
return redis.createClient(options);
}
module.exports = RedisCache;
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