node-ts-cache
node-ts-cache copied to clipboard
storage-node-fs with string parameter
Error when trying to cache a method with a string parameter
Example:
import { Cache, CacheContainer } from 'node-ts-cache';
import { NodeFsStorage } from 'node-ts-cache-storage-node-fs';
const userCache = new CacheContainer(new NodeFsStorage('cache/example.json'));
class MyService {
@Cache(userCache, { ttl: 10 })
public async getUser(name) {
if (name == 'name1') {
return { name: 'LONGGTEXTTTTTTTTTT' };
} else if (name == 'name2') {
return undefined;
}
}
}
const s = new MyService();
s.getUser('name1').then(console.log);
s.getUser('name2').then(console.log);
Error:
at JSON.parse (<anonymous>)
at NodeFsStorage.<anonymous> (/home/node_modules/node-ts-cache-storage-node-fs/dist/index.js:111:54)
The cache filed generated is invalid:
{"MyService:getUser:[\"name2\"]":{"meta":{"ttl":10000,"createdAt":1623514436658}}}content":{"name":"LONGGTEXTTTTTTTTTT"}}}
I think the error is only for storage-node-fs
because use one file for multiple promises this is a more simple example
const store = new NodeFsStorage('cache/example.json');
const keys = [...Array(5).keys()];
keys.forEach(async (i) => {
await store.setItem(`${i}`, 1234);
});
@MiguelSavignano Having a look on this.
@MiguelSavignano Could not reproduce this with your example. The generated json file seems to be OK.