Skip to content

Commit 6605bfc

Browse files
Refactor validateCache function
1 parent 6070b54 commit 6605bfc

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

src/storages/inLocalStorage/validateCache.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,42 @@ import { LOG_PREFIX } from './constants';
66
import type { SplitsCacheInLocal } from './SplitsCacheInLocal';
77
import { KeyBuilderCS } from '../KeyBuilderCS';
88

9-
/**
10-
* Clean cache if:
11-
* - it has expired, i.e., its `lastUpdated` timestamp is older than the given `expirationTimestamp`
12-
* - hash has changed, i.e., the SDK key, flags filter criteria or flags spec version was modified
13-
*/
14-
export function validateCache(settings: ISettings, keys: KeyBuilderCS, splits: SplitsCacheInLocal): boolean {
9+
function validateExpiration(settings: ISettings, keys: KeyBuilderCS) {
1510
const { log } = settings;
1611

17-
// Check expiration and clear cache if needed
12+
// Check expiration
1813
const expirationTimestamp = Date.now() - DEFAULT_CACHE_EXPIRATION_IN_MILLIS;
1914
let value: string | number | null = localStorage.getItem(keys.buildLastUpdatedKey());
2015
if (value !== null) {
2116
value = parseInt(value, 10);
22-
if (!isNaNNumber(value) && value < expirationTimestamp) splits.clear();
17+
if (!isNaNNumber(value) && value < expirationTimestamp) return true;
2318
}
2419

25-
// Check hash and clear cache if needed
20+
// Check hash
2621
const storageHashKey = keys.buildHashKey();
2722
const storageHash = localStorage.getItem(storageHashKey);
2823
const currentStorageHash = getStorageHash(settings);
2924

3025
if (storageHash !== currentStorageHash) {
3126
log.info(LOG_PREFIX + 'SDK key, flags filter criteria or flags spec version was modified. Updating cache');
3227
try {
33-
if (splits.getChangeNumber() > -1) splits.clear();
34-
3528
localStorage.setItem(storageHashKey, currentStorageHash);
3629
} catch (e) {
3730
log.error(LOG_PREFIX + e);
3831
}
32+
return true;
33+
}
34+
}
35+
36+
/**
37+
* Clean cache if:
38+
* - it has expired, i.e., its `lastUpdated` timestamp is older than the given `expirationTimestamp`
39+
* - hash has changed, i.e., the SDK key, flags filter criteria or flags spec version was modified
40+
*/
41+
export function validateCache(settings: ISettings, keys: KeyBuilderCS, splits: SplitsCacheInLocal): boolean {
42+
43+
if (validateExpiration(settings, keys)) {
44+
splits.clear();
3945
}
4046

4147
// Check if the cache is ready

0 commit comments

Comments
 (0)