Skip to content

Commit

Permalink
clearOnInit configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilianoSanchez committed Dec 18, 2024
1 parent 87fbc4f commit aca35fe
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/storages/KeyBuilderCS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export class KeyBuilderCS extends KeyBuilder implements MySegmentsKeyBuilder {
buildTillKey() {
return `${this.prefix}.${this.matchingKey}.segments.till`;
}

buildLastClear() {
return `${this.prefix}.lastClear`;
}
}

export function myLargeSegmentsKeyBuilder(prefix: string, matchingKey: string): MySegmentsKeyBuilder {
Expand Down
21 changes: 20 additions & 1 deletion src/storages/inLocalStorage/validateCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ function validateExpiration(options: SplitIO.InLocalStorageOptions, settings: IS
}
return true;
}

// Clear on init
if (options.clearOnInit) {
let value: string | number | null = localStorage.getItem(keys.buildLastClear());
if (value !== null) {
value = parseInt(value, 10);
if (!isNaNNumber(value) && value < Date.now() - MILLIS_IN_A_DAY) {
log.info(LOG_PREFIX + 'Clear on init was set and cache was cleared more than a day ago. Cleaning up cache');
return true;
}
}
}
}

/**
Expand All @@ -52,8 +64,15 @@ export function validateCache(options: SplitIO.InLocalStorageOptions, settings:
splits.clear();
segments.clear();
largeSegments.clear();

// Update last clear timestamp
try {
localStorage.setItem(keys.buildLastClear(), Date.now() + '');
} catch (e) {
settings.log.error(LOG_PREFIX + e);
}
}

// Check if the cache is ready
// Check if ready from cache
return splits.getChangeNumber() > -1;
}

0 comments on commit aca35fe

Please sign in to comment.