Skip to content

Commit aca35fe

Browse files
clearOnInit configuration
1 parent 87fbc4f commit aca35fe

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/storages/KeyBuilderCS.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ export class KeyBuilderCS extends KeyBuilder implements MySegmentsKeyBuilder {
4343
buildTillKey() {
4444
return `${this.prefix}.${this.matchingKey}.segments.till`;
4545
}
46+
47+
buildLastClear() {
48+
return `${this.prefix}.lastClear`;
49+
}
4650
}
4751

4852
export function myLargeSegmentsKeyBuilder(prefix: string, matchingKey: string): MySegmentsKeyBuilder {

src/storages/inLocalStorage/validateCache.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ function validateExpiration(options: SplitIO.InLocalStorageOptions, settings: IS
3939
}
4040
return true;
4141
}
42+
43+
// Clear on init
44+
if (options.clearOnInit) {
45+
let value: string | number | null = localStorage.getItem(keys.buildLastClear());
46+
if (value !== null) {
47+
value = parseInt(value, 10);
48+
if (!isNaNNumber(value) && value < Date.now() - MILLIS_IN_A_DAY) {
49+
log.info(LOG_PREFIX + 'Clear on init was set and cache was cleared more than a day ago. Cleaning up cache');
50+
return true;
51+
}
52+
}
53+
}
4254
}
4355

4456
/**
@@ -52,8 +64,15 @@ export function validateCache(options: SplitIO.InLocalStorageOptions, settings:
5264
splits.clear();
5365
segments.clear();
5466
largeSegments.clear();
67+
68+
// Update last clear timestamp
69+
try {
70+
localStorage.setItem(keys.buildLastClear(), Date.now() + '');
71+
} catch (e) {
72+
settings.log.error(LOG_PREFIX + e);
73+
}
5574
}
5675

57-
// Check if the cache is ready
76+
// Check if ready from cache
5877
return splits.getChangeNumber() > -1;
5978
}

0 commit comments

Comments
 (0)