@ -4,72 +4,30 @@ module.exports = function (opts) {
const LRU = require ( 'lru-cache' ) ;
const pubsub = require ( './pubsub' ) ;
// lru-cache@7 deprecations
const winston = require ( 'winston' ) ;
const chalk = require ( 'chalk' ) ;
const cache = new LRU ( opts ) ;
// sometimes we kept passing in `length` with no corresponding `maxSize`.
// This is now enforced in v7; drop superfluous property
if ( opts . hasOwnProperty ( 'length' ) && ! opts . hasOwnProperty ( 'maxSize' ) ) {
winston . warn ( ` [cache/init( ${ opts . name } )] ${ chalk . white . bgRed . bold ( 'DEPRECATION' ) } ${ chalk . yellow ( 'length' ) } was passed in without a corresponding ${ chalk . yellow ( 'maxSize' ) } . Both are now required as of lru-cache@7.0.0. ` ) ;
delete opts . length ;
}
const deprecations = new Map ( [
[ 'stale' , 'allowStale' ] ,
[ 'maxAge' , 'ttl' ] ,
[ 'length' , 'sizeCalculation' ] ,
] ) ;
deprecations . forEach ( ( newProp , oldProp ) => {
if ( opts . hasOwnProperty ( oldProp ) && ! opts . hasOwnProperty ( newProp ) ) {
winston . warn ( ` [cache/init ( ${ opts . name } )] ${ chalk . white . bgRed . bold ( 'DEPRECATION' ) } The option ${ chalk . yellow ( oldProp ) } has been deprecated as of lru-cache@7.0.0. Please change this to ${ chalk . yellow ( newProp ) } instead. ` ) ;
opts [ newProp ] = opts [ oldProp ] ;
delete opts [ oldProp ] ;
}
} ) ;
const lruCache = new LRU ( opts ) ;
const cache = { } ;
cache . name = opts . name ;
cache . hits = 0 ;
cache . misses = 0 ;
cache . enabled = opts . hasOwnProperty ( 'enabled' ) ? opts . enabled : true ;
const cacheSet = lruCache . set ;
// backwards compatibility
const propertyMap = new Map ( [
[ 'length' , 'calculatedSize' ] ,
[ 'max' , 'max' ] ,
[ 'maxSize' , 'maxSize' ] ,
[ 'itemCount' , 'size' ] ,
] ) ;
propertyMap . forEach ( ( lruProp , cacheProp ) => {
Object . defineProperty ( cache , cacheProp , {
get : function ( ) {
return lruCache [ lruProp ] ;
} ,
configurable : true ,
enumerable : true ,
} ) ;
} ) ;
const cacheSet = cache . set ;
const cacheGet = cache . get ;
const cacheDel = cache . del ;
const cacheReset = cache . reset ;
cache . set = function ( key , value , ttl ) {
cache . set = function ( key , value , maxAge ) {
if ( ! cache . enabled ) {
return ;
}
const opts = { } ;
if ( ttl ) {
opts . ttl = ttl ;
}
cacheSet . apply ( lruCache , [ key , value , opts ] ) ;
cacheSet . apply ( cache , [ key , value , maxAge ] ) ;
} ;
cache . get = function ( key ) {
if ( ! cache . enabled ) {
return undefined ;
}
const data = lruCache. get ( key ) ;
const data = cacheGet . apply ( cache , [ key ] ) ;
if ( data === undefined ) {
cache . misses += 1 ;
} else {
@ -83,18 +41,16 @@ module.exports = function (opts) {
keys = [ keys ] ;
}
pubsub . publish ( ` ${ cache . name } :cache:del ` , keys ) ;
keys . forEach ( key => lruCache. delete ( key ) ) ;
keys . forEach ( key => cacheDel. apply ( cache , [ key ] ) ) ;
} ;
cache . delete = cache . del ;
cache . reset = function ( ) {
pubsub . publish ( ` ${ cache . name } :cache:reset ` ) ;
localReset ( ) ;
} ;
cache . clear = cache . reset ;
function localReset ( ) {
lruCache. clear ( ) ;
cacheReset. apply ( cache ) ;
cache . hits = 0 ;
cache . misses = 0 ;
}
@ -105,7 +61,7 @@ module.exports = function (opts) {
pubsub . on ( ` ${ cache . name } :cache:del ` , ( keys ) => {
if ( Array . isArray ( keys ) ) {
keys . forEach ( key => lruCache. delete ( key ) ) ;
keys . forEach ( key => cacheDel. apply ( cache , [ key ] ) ) ;
}
} ) ;
@ -131,13 +87,5 @@ module.exports = function (opts) {
return unCachedKeys ;
} ;
cache . dump = function ( ) {
return lruCache . dump ( ) ;
} ;
cache . peek = function ( key ) {
return lruCache . peek ( key ) ;
} ;
return cache ;
} ;