Skip to content

Commit 34f00c2

Browse files
committed
refactor: Extract cache key mechanism.
1 parent c99b62a commit 34f00c2

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

packages/sdk/src/core/flags/flags-manager.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -294,16 +294,18 @@ export class CoreFlagsManager implements FlagsManager {
294294
const apiUrl = this.config.apiUrl ?? "https://api.databuddy.cc";
295295
const params = buildQueryParams(this.config, user);
296296

297+
const ttl = this.config.cacheTtl ?? 60_000;
298+
const staleTime = this.config.staleTime ?? ttl / 2;
299+
297300
try {
298301
const flags = await fetchAllFlagsApi(apiUrl, params);
302+
const flagCacheEntries = Object.entries(flags).map(([key, result]) => ({
303+
cacheKey: getCacheKey(key, user ?? this.config.user),
304+
cacheEntry: createCacheEntry(result, ttl, staleTime),
305+
}));
299306

300-
// Update cache with all flags
301-
const ttl = this.config.cacheTtl ?? 60_000;
302-
const staleTime = this.config.staleTime ?? ttl / 2;
303-
304-
for (const [key, result] of Object.entries(flags)) {
305-
const cacheKey = getCacheKey(key, user ?? this.config.user);
306-
this.cache.set(cacheKey, createCacheEntry(result, ttl, staleTime));
307+
for (const { cacheKey, cacheEntry } of flagCacheEntries) {
308+
this.cache.set(cacheKey, cacheEntry);
307309
}
308310

309311
this.ready = true;

0 commit comments

Comments
 (0)