Skip to content

Commit 36b853b

Browse files
authored
docs: add docs about caching (#269)
1 parent 60d4b08 commit 36b853b

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,25 @@ if (enabled) {
7878
const flagEvaluation = await confidence.evaluateFlag('tutorial-feature', {});
7979
```
8080

81+
#### Caching
82+
83+
Flag evaluations are cached in memory on the Confidence instance with the evaluation context and flag name as a cache key.
84+
This is done to reduce network calls when evaluating multiple flags using the same context.
85+
86+
```ts
87+
const confidence = Confidence.create({...});
88+
const flag = confidence.getFlag('flag', {})
89+
// subsequent calls to getFlag will return the same value
90+
```
91+
92+
If you need to always fetch the latest flag values (e.g., for testing, debugging or an other use case),
93+
you can bypass the cache by always get a fresh Confidence instance (and an empty cache):
94+
95+
```ts
96+
const confidence = Confidence.create({...});
97+
const flag = confidence.withContext({}).getFlag('flag', {})
98+
```
99+
81100
# Contributions and Development
82101

83102
We'd love to get patches from you! See [Contributing](CONTRIBUTING.md) for details.

packages/sdk/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,25 @@ const message = messageEvaluation.value;
108108

109109
In a client application (where `environment` is set to `client`), the SDK fetches and caches all flags when the context is updated. This means the flags can be accessed synchronously after that.
110110

111+
### Caching
112+
113+
Flag evaluations are cached in memory on the Confidence instance with the evaluation context and flag name as a cache key.
114+
This is done to reduce network calls when evaluating multiple flags using the same context.
115+
116+
```ts
117+
const confidence = Confidence.create({...});
118+
const flag = confidence.getFlag('flag', {})
119+
// subsequent calls to getFlag will return the same value
120+
```
121+
122+
If you need to always fetch the latest flag values (e.g., for testing, debugging or an other use case),
123+
you can bypass the cache by always get a fresh Confidence instance (and an empty cache):
124+
125+
```ts
126+
const confidence = Confidence.create({...});
127+
const flag = confidence.withContext({}).getFlag('flag', {})
128+
```
129+
111130
## Event tracking
112131

113132
Use `confidence.track()` from any Confidence instance to track an event in Confidence. Any context data set on the instance will be appended to the tracking event.

0 commit comments

Comments
 (0)