docs.nestjs.com icon indicating copy to clipboard operation
docs.nestjs.com copied to clipboard

docs(caching): add note about of cache-manager

Open jpham005 opened this issue 2 years ago • 0 comments

Since cache manager's In-memory store uses lodash.clonedeep internally, it can only store values of types supported by 'lodash'.

PR Checklist

Please check if your PR fulfills the following requirements:

  • [x] The commit message follows our guidelines: https://github.com/nestjs/docs.nestjs.com/blob/master/CONTRIBUTING.md

PR Type

What kind of change does this PR introduce?

  • [ ] Bugfix
  • [ ] Feature
  • [ ] Code style update (formatting, local variables)
  • [ ] Refactoring (no functional changes, no api changes)
  • [ ] Build related changes
  • [x] Docs
  • [ ] Other... Please describe:

What is the current behavior?

  • Property descriptors, setters, getters, and similar metadata-like features are not duplicated. For example, if an object is marked readonly with a property descriptor, it will be read/write in the duplicate, since that's the default. mdn-Structured_clone_algorithm
import { ObjectId } from "bson"; // import package to test invalid type which uses 
import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { Cache } from 'cache-manager';

export class ExampleService {
  constructor (
    @Inject(CACHE_MANAGER)
    private cacheManagerService: Cache;
  ) {}

  async generatesNodeInternalError() {
    const objectId = new ObjectId("some id..."); // has OwnPropertyDescriptors
    const map = new Map();
    map.set('mapKey', objectId);  
    
    const cacheKey = 'cacheKey';

    await this.cacheManagerService.set(cacheKey, map);
    // Since `console.log` uses custom inspect function of ObjectId,
    // which requires properties in original object, an exception will occur below.
    console.log(await this.cacheManagerService.get(cacheKey));
  }
}

Issue Number: N/A

What is the new behavior?

Does this PR introduce a breaking change?

  • [ ] Yes
  • [x] No

Other information

  1. @nestjs/cache-module is not passing isCachable to node-cache-manager. link
  2. node-cache-manager clones value by lodash.clonedeep. link
  3. Then call set of lru-cache. After here, cached value's PropertyDescriptors are omitted. link

jpham005 avatar Jul 05 '23 14:07 jpham005