Reputation: 1429
I'm using node-cache-manager for managing the Node.js cache.
I've successfully added an item to the cache:
import cacheManager from 'cache-manager';
const memoryCache = cacheManager.caching({store: 'memory', max: 100, ttl: 3600/*seconds*/});
memoryCache.set('testItem', { 'key': 'testValue'}, {ttl: 36000}, (err) => {
if (err) { throw err; }
});
But when I want to retrieve an item from the cache it responds with Result: [Object Object]
This is the code:
let cacheResult = await memoryCache.get('testItem');
console.log('Result: ' + { cacheResult });
Upvotes: 0
Views: 675
Reputation: 36
Aloha !
You have defined the variable as a JSON OBJECT and therefore Javascript considers it as an Object [Object Object].
To avoid this, you have to turn JSON Object into string You have to do:
memoryCache.set('testItem', JSON.stringify({ 'key': 'testValue'}), {ttl: 36000}, (err) => {
if (err) { throw err; }
});
And when you want to retrieve the content of the variable
let cacheResult = await JSON.parse(memoryCache.get('testItem'), true);
Live Exemple:
var json = JSON.stringify({"name":"John", "age":30, "city":"New York"});
var obj = JSON.parse(json, true);
console.log(obj);
console.log('Name of user: ' + obj.name)
Upvotes: 1