tdeekens / flopflip

🎚Flip or flop features in your React application in real-time backed by flag provider of your choice 🚦
https://techblog.commercetools.com/embracing-real-time-feature-toggling-in-your-react-application-a5e6052716a9
MIT License
403 stars 40 forks source link

refactor: cache keys to be bound to adapter context #1922

Closed tdeekens closed 4 months ago

tdeekens commented 4 months ago

Summary

This refactors the cache's key for flags to be a hashed version of the adapter's context.

Description

When caching lazily it is important to fall back to a hot cache as often as possible. Otherwise a user would be served a stale cache for flags.

Taking for example the flow of:

  1. User logs in
  2. Views project A
  3. In project A feature is enabled
  4. User navigates to project B
  5. In project B feature is disabled

With a lazy acting cache, in the above flow the user at 5. would still see an enabled feature. This is rooted in the fact that the cache does not synchronize its state until another hard reload (or unmounting of flopflip).

The reason for this behaviour is that at any point flopflip's cache is bound - without this change - to a user's key. Any user can only have one set of cached flags.

This change proposes to take the entire context of the adapter into account for the cache key. This allows a user to have n caches depending on the number of contexts the user is in. This allows a "hot cache" to be restored more likely and flopflip not falling back onto a cold cache often.

changeset-bot[bot] commented 4 months ago

🦋 Changeset detected

Latest commit: 5334e4c2c5f86bab0fcf82ab3cbef96c2515135f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 17 packages | Name | Type | | ------------------------------ | ----- | | @flopflip/cache | Patch | | @flopflip/graphql-adapter | Patch | | @flopflip/http-adapter | Patch | | @flopflip/launchdarkly-adapter | Patch | | @flopflip/react | Patch | | @flopflip/react-broadcast | Patch | | @flopflip/react-redux | Patch | | @flopflip/adapter-utilities | Patch | | @flopflip/combine-adapters | Patch | | @flopflip/cypress-plugin | Patch | | @flopflip/localstorage-adapter | Patch | | @flopflip/localstorage-cache | Patch | | @flopflip/memory-adapter | Patch | | @flopflip/sessionstorage-cache | Patch | | @flopflip/splitio-adapter | Patch | | @flopflip/test-utils | Patch | | @flopflip/types | Patch |

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

codecov[bot] commented 4 months ago

Codecov Report

Attention: Patch coverage is 89.47368% with 2 lines in your changes missing coverage. Please review.

Project coverage is 93.88%. Comparing base (44a82fd) to head (5334e4c).

Additional details and impacted files [![Impacted file tree graph](https://app.codecov.io/gh/tdeekens/flopflip/pull/1922/graphs/tree.svg?width=650&height=150&src=pr&token=QB77hgphUg&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Tobias+Deekens)](https://app.codecov.io/gh/tdeekens/flopflip/pull/1922?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Tobias+Deekens) ```diff @@ Coverage Diff @@ ## main #1922 +/- ## ========================================== + Coverage 93.79% 93.88% +0.08% ========================================== Files 109 109 Lines 1758 1765 +7 Branches 284 282 -2 ========================================== + Hits 1649 1657 +8 + Misses 99 98 -1 Partials 10 10 ``` | [Files](https://app.codecov.io/gh/tdeekens/flopflip/pull/1922?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Tobias+Deekens) | Coverage Δ | | |---|---|---| | [packages/cache/src/cache/cache.ts](https://app.codecov.io/gh/tdeekens/flopflip/pull/1922?src=pr&el=tree&filepath=packages%2Fcache%2Fsrc%2Fcache%2Fcache.ts&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Tobias+Deekens#diff-cGFja2FnZXMvY2FjaGUvc3JjL2NhY2hlL2NhY2hlLnRz) | `88.70% <100.00%> (+1.91%)` | :arrow_up: | | [...ckages/launchdarkly-adapter/src/adapter/adapter.ts](https://app.codecov.io/gh/tdeekens/flopflip/pull/1922?src=pr&el=tree&filepath=packages%2Flaunchdarkly-adapter%2Fsrc%2Fadapter%2Fadapter.ts&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Tobias+Deekens#diff-cGFja2FnZXMvbGF1bmNoZGFya2x5LWFkYXB0ZXIvc3JjL2FkYXB0ZXIvYWRhcHRlci50cw==) | `96.51% <ø> (+0.53%)` | :arrow_up: | | [packages/graphql-adapter/src/adapter/adapter.ts](https://app.codecov.io/gh/tdeekens/flopflip/pull/1922?src=pr&el=tree&filepath=packages%2Fgraphql-adapter%2Fsrc%2Fadapter%2Fadapter.ts&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Tobias+Deekens#diff-cGFja2FnZXMvZ3JhcGhxbC1hZGFwdGVyL3NyYy9hZGFwdGVyL2FkYXB0ZXIudHM=) | `86.36% <75.00%> (ø)` | | | [packages/http-adapter/src/adapter/adapter.ts](https://app.codecov.io/gh/tdeekens/flopflip/pull/1922?src=pr&el=tree&filepath=packages%2Fhttp-adapter%2Fsrc%2Fadapter%2Fadapter.ts&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Tobias+Deekens#diff-cGFja2FnZXMvaHR0cC1hZGFwdGVyL3NyYy9hZGFwdGVyL2FkYXB0ZXIudHM=) | `85.93% <75.00%> (ø)` | |