Comparing version 0.9.2 to 0.9.5
44
index.js
'use strict'; | ||
const memcached = require('memjs') | ||
const memcached = require('memcached') | ||
const LRU = require('lru-cache'); | ||
const memcached_api = process.env.MEMCACHIER_SERVERS || 'localhost:11211' | ||
@@ -11,6 +12,6 @@ /** | ||
*/ | ||
function Cache({type = 'memcached', prefix = 'client'}) { | ||
function Cache({type = 'memcached', prefix = 'client', expiration = 600}) { | ||
this.type = type; | ||
this.prefix = prefix; | ||
this.cache = this[type](); | ||
this.cache = this[type](expiration); | ||
} | ||
@@ -37,5 +38,26 @@ | ||
/** | ||
* | ||
* @param key | ||
* @returns {Promise} | ||
*/ | ||
Cache.prototype.delete = function({key}) { | ||
return new Promise((resolve, reject) =>{ | ||
if(!key) reject('key {string} is not defined'); | ||
// if type lru usually return value | ||
if(this.type === 'lru' ) { | ||
let value = this.cache.del(this.prefix+key); | ||
return value ? resolve(value) : reject(value); | ||
} | ||
this.cache.del(this.prefix+key, function (err, val) { | ||
if(err) console.log(err) | ||
err || !val ? reject(err ? 'memcached is not runing!' : val) : resolve(val) | ||
}) | ||
}) | ||
}; | ||
/** | ||
* | ||
* @param key | ||
* @param value | ||
@@ -52,3 +74,3 @@ * @param expires | ||
// memcached return callback value | ||
this.cache.set(this.prefix+key, value, {expires}, function (err, val) { | ||
this.cache.set(this.prefix+key, value, expires, function (err, val) { | ||
if(err) console.log(err) | ||
@@ -64,6 +86,6 @@ err ? resolve(value) : resolve(val) | ||
*/ | ||
Cache.prototype.lru = () => { | ||
Cache.prototype.lru = ({expiration}) => { | ||
return LRU({ | ||
max: 1000, | ||
maxAge: 1000 * 60 * 10 // 10 min cache | ||
maxAge: 1000 * expiration // 10 min cache | ||
}) | ||
@@ -74,6 +96,12 @@ }; | ||
*/ | ||
Cache.prototype.memcached = () => { | ||
return memcached.Client.create(); | ||
Cache.prototype.memcached = ({expiration}) => { | ||
// return memcached.Client.create(); | ||
try { | ||
return new Memcached(memcached_api, {retries:10,retry:10000,remove:true, maxExpiration: expiration }); | ||
} catch (e){ | ||
console.log(e) | ||
return e | ||
} | ||
}; | ||
module.exports = Cache; |
{ | ||
"name": "ssr-cache", | ||
"version": "0.9.2", | ||
"version": "0.9.5", | ||
"description": "Api Serve-Side Cache Wrapper for Lru and Memcached", | ||
@@ -13,4 +13,4 @@ "main": "index.js", | ||
"lru-cache": "^4.1.1", | ||
"memjs": "^1.0.0" | ||
"memcached": "^2.2.2" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
AI-detected possible typosquat
Supply chain riskAI has identified this package as a potential typosquat of a more popular package. This suggests that the package may be intentionally mimicking another package's name, description, or other metadata.
Found 1 instance in 1 package
4944
95
0
2
+ Addedmemcached@^2.2.2
+ Addedconnection-parse@0.0.7(transitive)
+ Addedhashring@3.2.0(transitive)
+ Addedjackpot@0.0.6(transitive)
+ Addedmemcached@2.2.2(transitive)
+ Addedretry@0.6.0(transitive)
+ Addedsimple-lru-cache@0.0.2(transitive)
- Removedmemjs@^1.0.0
- Removedmemjs@1.3.2(transitive)