node-ts-cache icon indicating copy to clipboard operation
node-ts-cache copied to clipboard

storage-node-fs with string parameter

Open MiguelSavignano opened this issue 3 years ago • 3 comments

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"}}}

MiguelSavignano avatar Jun 12 '21 14:06 MiguelSavignano

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 avatar Jun 12 '21 17:06 MiguelSavignano

@MiguelSavignano Having a look on this.

havsar avatar Jun 28 '21 07:06 havsar

@MiguelSavignano Could not reproduce this with your example. The generated json file seems to be OK.

havsar avatar Oct 08 '21 13:10 havsar