Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support InMemoryCache({ freezeResults: true }) to help enforce immutability. #4514

Merged
merged 3 commits into from
Feb 28, 2019

Commits on Feb 28, 2019

  1. Support InMemoryCache({ freezeResults: true }) to help enforce immuta…

    …bility.
    
    Part of the plan I outlined in this comment:
    #4464 (comment)
    
    If we could trust application code not to modify cache results, we
    wouldn't have to save deep snapshots of past results in order to implement
    isDifferentFromLastResult correctly (see #4069).
    
    Aside: why doesn't the cache just return defensive copies of all results?
    #4031 (comment)
    
    While you might agree that immutability is a worthwhile aspiration, it can
    be hard to maintain that discipline across your entire application over
    time, especially in a team of multiple developers.
    
    This commit implements a new freezeResults option for the InMemoryCache
    constructor, which (when true) causes all cache results to be frozen in
    development, so you can more easily detect accidental mutations.
    
    Note: mutating frozen objects only throws in strict mode, whereas it fails
    silently in non-strict code. ECMAScript module code automatically runs in
    strict mode, and most module transforms add "use strict" at the top of the
    generated code, so you're probably already using strict mode everywhere,
    though you might want to double-check.
    
    The beauty of this implementation is that it does not need to repeatedly
    freeze entire results, because it can shallow-freeze the root of each
    subtree when that object is first created.
    
    Thanks to result caching, those frozen objects can be shared between
    multiple different result trees without any additional freezing, and the
    entire result always ends up deeply frozen.
    
    The freezing happens only in non-production environments, so there is no
    runtime cost to using { freezeResults: true } in production. Please keep
    this in mind when benchmarking cache performance!
    benjamn committed Feb 28, 2019
    Configuration menu
    Copy the full SHA
    ebb66d1 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    f7417c1 View commit details
    Browse the repository at this point in the history
  3. Mention PR #4514 in CHANGELOG.md.

    benjamn committed Feb 28, 2019
    Configuration menu
    Copy the full SHA
    b1acbce View commit details
    Browse the repository at this point in the history