@@ -11,16 +11,17 @@ import SplitIO from '../../../types/splitio';
1111const DEFAULT_CACHE_EXPIRATION_IN_DAYS = 10 ;
1212const MILLIS_IN_A_DAY = 86400000 ;
1313
14- function validateExpiration ( options : SplitIO . InLocalStorageOptions , settings : ISettings , keys : KeyBuilderCS ) {
14+ function validateExpiration ( options : SplitIO . InLocalStorageOptions , settings : ISettings , keys : KeyBuilderCS , currentTimestamp : number ) {
1515 const { log } = settings ;
1616
1717 // Check expiration
18- const expirationTimestamp = Date . now ( ) - MILLIS_IN_A_DAY * ( isFiniteNumber ( options . expirationDays ) && options . expirationDays >= 1 ? options . expirationDays : DEFAULT_CACHE_EXPIRATION_IN_DAYS ) ;
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 ;
1920 let value : string | number | null = localStorage . getItem ( keys . buildLastUpdatedKey ( ) ) ;
2021 if ( value !== null ) {
2122 value = parseInt ( value , 10 ) ;
2223 if ( ! isNaNNumber ( value ) && value < expirationTimestamp ) {
23- log . info ( LOG_PREFIX + 'Cache expired. Cleaning up cache' ) ;
24+ log . info ( LOG_PREFIX + 'Cache expired more than ' + cacheExpirationInDays + ' days ago . Cleaning up cache') ;
2425 return true ;
2526 }
2627 }
@@ -45,7 +46,7 @@ function validateExpiration(options: SplitIO.InLocalStorageOptions, settings: IS
4546 let value : string | number | null = localStorage . getItem ( keys . buildLastClear ( ) ) ;
4647 if ( value !== null ) {
4748 value = parseInt ( value , 10 ) ;
48- if ( ! isNaNNumber ( value ) && value < Date . now ( ) - MILLIS_IN_A_DAY ) {
49+ if ( ! isNaNNumber ( value ) && value < currentTimestamp - MILLIS_IN_A_DAY ) {
4950 log . info ( LOG_PREFIX + 'Clear on init was set and cache was cleared more than a day ago. Cleaning up cache' ) ;
5051 return true ;
5152 }
@@ -60,14 +61,16 @@ function validateExpiration(options: SplitIO.InLocalStorageOptions, settings: IS
6061 */
6162export function validateCache ( options : SplitIO . InLocalStorageOptions , settings : ISettings , keys : KeyBuilderCS , splits : SplitsCacheInLocal , segments : MySegmentsCacheInLocal , largeSegments : MySegmentsCacheInLocal ) : boolean {
6263
63- if ( validateExpiration ( options , settings , keys ) ) {
64+ const currentTimestamp = Date . now ( ) ;
65+
66+ if ( validateExpiration ( options , settings , keys , currentTimestamp ) ) {
6467 splits . clear ( ) ;
6568 segments . clear ( ) ;
6669 largeSegments . clear ( ) ;
6770
6871 // Update last clear timestamp
6972 try {
70- localStorage . setItem ( keys . buildLastClear ( ) , Date . now ( ) + '' ) ;
73+ localStorage . setItem ( keys . buildLastClear ( ) , currentTimestamp + '' ) ;
7174 } catch ( e ) {
7275 settings . log . error ( LOG_PREFIX + e ) ;
7376 }
0 commit comments