const NC = require( "node-cache" );
// run the same code with and without `useClones`
( [ true, false ] ).forEach( ( useClones ) => {
const cache = new NC( { useClones: useClones } );
const complex = {
a: [ 13, 42, 666 ],
foo: "Alice",
bar: "Bob"
}
const simple = "Ding Dong"
cache.set( "A", complex )
cache.set( "B", complex )
cache.set( "X", simple )
cache.set( "Y", simple )
// useClones = true : set data of "A". Should not effect "B"
// useClones = false : set data of "A". Will effect "B"
// because both holding just a refernce to the same data
const a = cache.get( "A" )
a.foo = "John?"
console.log( "Complex - useClones = " + useClones + " --- a.foo: '" + a.foo + "' != b.foo: '" + cache.get( "B" ).foo + "'" )
// In case of simple values like strings, numbers, booleans useClones will not have any effect
let x = cache.get( "X" )
x = "Bing Bong"
console.log( "Simple - useClones = " + useClones + " ---- x : '" + x + "' != y: '" + cache.get( "Y" ) + "'" )
} )