@@ -15,12 +15,11 @@ function validateExpiration(options: SplitIO.InLocalStorageOptions, settings: IS
1515 const { log } = settings ;
1616
1717 // Check expiration
18- const cacheExpirationInDays = isFiniteNumber ( options . expirationDays ) && options . expirationDays >= 1 ? options . expirationDays : DEFAULT_CACHE_EXPIRATION_IN_DAYS ;
19- const expirationTimestamp = currentTimestamp - MILLIS_IN_A_DAY * cacheExpirationInDays ;
20- let value : string | number | null = localStorage . getItem ( keys . buildLastUpdatedKey ( ) ) ;
21- if ( value !== null ) {
22- value = parseInt ( value , 10 ) ;
23- if ( ! isNaNNumber ( value ) && value < expirationTimestamp ) {
18+ const lastUpdatedTimestamp = parseInt ( localStorage . getItem ( keys . buildLastUpdatedKey ( ) ) as string , 10 ) ;
19+ if ( ! isNaNNumber ( lastUpdatedTimestamp ) ) {
20+ const cacheExpirationInDays = isFiniteNumber ( options . expirationDays ) && options . expirationDays >= 1 ? options . expirationDays : DEFAULT_CACHE_EXPIRATION_IN_DAYS ;
21+ const expirationTimestamp = currentTimestamp - MILLIS_IN_A_DAY * cacheExpirationInDays ;
22+ if ( lastUpdatedTimestamp < expirationTimestamp ) {
2423 log . info ( LOG_PREFIX + 'Cache expired more than ' + cacheExpirationInDays + ' days ago. Cleaning up cache' ) ;
2524 return true ;
2625 }
@@ -32,7 +31,7 @@ function validateExpiration(options: SplitIO.InLocalStorageOptions, settings: IS
3231 const currentStorageHash = getStorageHash ( settings ) ;
3332
3433 if ( storageHash !== currentStorageHash ) {
35- log . info ( LOG_PREFIX + 'SDK key, flags filter criteria or flags spec version was modified . Cleaning up cache' ) ;
34+ log . info ( LOG_PREFIX + 'SDK key, flags filter criteria or flags spec version has changed . Cleaning up cache' ) ;
3635 try {
3736 localStorage . setItem ( storageHashKey , currentStorageHash ) ;
3837 } catch ( e ) {
@@ -43,13 +42,11 @@ function validateExpiration(options: SplitIO.InLocalStorageOptions, settings: IS
4342
4443 // Clear on init
4544 if ( options . clearOnInit ) {
46- let value : string | number | null = localStorage . getItem ( keys . buildLastClear ( ) ) ;
47- if ( value !== null ) {
48- value = parseInt ( value , 10 ) ;
49- if ( ! isNaNNumber ( value ) && value < currentTimestamp - MILLIS_IN_A_DAY ) {
50- log . info ( LOG_PREFIX + 'Clear on init was set and cache was cleared more than a day ago. Cleaning up cache' ) ;
51- return true ;
52- }
45+ const lastClearTimestamp = parseInt ( localStorage . getItem ( keys . buildLastClear ( ) ) as string , 10 ) ;
46+
47+ if ( isNaNNumber ( lastClearTimestamp ) || lastClearTimestamp < currentTimestamp - MILLIS_IN_A_DAY ) {
48+ log . info ( LOG_PREFIX + 'clearOnInit was set and cache was not cleared in the last 24 hours. Cleaning up cache' ) ;
49+ return true ;
5350 }
5451 }
5552}
@@ -58,6 +55,8 @@ function validateExpiration(options: SplitIO.InLocalStorageOptions, settings: IS
5855 * Clean cache if:
5956 * - it has expired, i.e., its `lastUpdated` timestamp is older than the given `expirationTimestamp`
6057 * - hash has changed, i.e., the SDK key, flags filter criteria or flags spec version was modified
58+ *
59+ * @returns `true` if cache is ready to be used, `false` otherwise (cache was cleared or there is no cache)
6160 */
6261export function validateCache ( options : SplitIO . InLocalStorageOptions , settings : ISettings , keys : KeyBuilderCS , splits : SplitsCacheInLocal , segments : MySegmentsCacheInLocal , largeSegments : MySegmentsCacheInLocal ) : boolean {
6362
0 commit comments