Skip to content

Commit 956c1df

Browse files
Handle no cache: cache should not be clearer
1 parent 6451cda commit 956c1df

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/storages/inLocalStorage/validateCache.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ import SplitIO from '../../../types/splitio';
1111
const DEFAULT_CACHE_EXPIRATION_IN_DAYS = 10;
1212
const MILLIS_IN_A_DAY = 86400000;
1313

14-
function validateExpiration(options: SplitIO.InLocalStorageOptions, settings: ISettings, keys: KeyBuilderCS, currentTimestamp: number) {
14+
/**
15+
* Validates if cache should be cleared and sets the cache `hash` if needed.
16+
*
17+
* @returns `true` if cache should be cleared, `false` otherwise
18+
*/
19+
function validateExpiration(options: SplitIO.InLocalStorageOptions, settings: ISettings, keys: KeyBuilderCS, currentTimestamp: number, isThereCache: boolean) {
1520
const { log } = settings;
1621

1722
// Check expiration
@@ -31,13 +36,16 @@ function validateExpiration(options: SplitIO.InLocalStorageOptions, settings: IS
3136
const currentStorageHash = getStorageHash(settings);
3237

3338
if (storageHash !== currentStorageHash) {
34-
log.info(LOG_PREFIX + 'SDK key, flags filter criteria or flags spec version has changed. Cleaning up cache');
3539
try {
3640
localStorage.setItem(storageHashKey, currentStorageHash);
3741
} catch (e) {
3842
log.error(LOG_PREFIX + e);
3943
}
40-
return true;
44+
if (isThereCache) {
45+
log.info(LOG_PREFIX + 'SDK key, flags filter criteria or flags spec version has changed. Cleaning up cache');
46+
return true;
47+
}
48+
return false; // No cache to clear
4149
}
4250

4351
// Clear on init
@@ -54,15 +62,17 @@ function validateExpiration(options: SplitIO.InLocalStorageOptions, settings: IS
5462
/**
5563
* Clean cache if:
5664
* - it has expired, i.e., its `lastUpdated` timestamp is older than the given `expirationTimestamp`
57-
* - hash has changed, i.e., the SDK key, flags filter criteria or flags spec version was modified
65+
* - its hash has changed, i.e., the SDK key, flags filter criteria or flags spec version was modified
66+
* - `clearOnInit` was set and cache was not cleared in the last 24 hours
5867
*
5968
* @returns `true` if cache is ready to be used, `false` otherwise (cache was cleared or there is no cache)
6069
*/
6170
export function validateCache(options: SplitIO.InLocalStorageOptions, settings: ISettings, keys: KeyBuilderCS, splits: SplitsCacheInLocal, segments: MySegmentsCacheInLocal, largeSegments: MySegmentsCacheInLocal): boolean {
6271

6372
const currentTimestamp = Date.now();
73+
const isThereCache = splits.getChangeNumber() > -1;
6474

65-
if (validateExpiration(options, settings, keys, currentTimestamp)) {
75+
if (validateExpiration(options, settings, keys, currentTimestamp, isThereCache)) {
6676
splits.clear();
6777
segments.clear();
6878
largeSegments.clear();
@@ -73,8 +83,10 @@ export function validateCache(options: SplitIO.InLocalStorageOptions, settings:
7383
} catch (e) {
7484
settings.log.error(LOG_PREFIX + e);
7585
}
86+
87+
return false;
7688
}
7789

7890
// Check if ready from cache
79-
return splits.getChangeNumber() > -1;
91+
return isThereCache;
8092
}

0 commit comments

Comments
 (0)