Skip to content

Commit e13f819

Browse files
Move expirationTimestamp logic inside validateCache method
1 parent ffd724d commit e13f819

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

src/storages/inLocalStorage/SplitsCacheInLocal.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { LOG_PREFIX } from './constants';
77
import { ISettings } from '../../types';
88
import { getStorageHash } from '../KeyBuilder';
99
import { setToArray } from '../../utils/lang/sets';
10+
import { DEFAULT_CACHE_EXPIRATION_IN_MILLIS } from '../../utils/constants/browser';
1011

1112
/**
1213
* ISplitsCacheSync implementation that stores split definitions in browser LocalStorage.
@@ -26,16 +27,17 @@ export class SplitsCacheInLocal extends AbstractSplitsCacheSync {
2627
}
2728

2829
/**
29-
* Clean Splits cache if its `lastUpdated` timestamp is older than the given `expirationTimestamp`,
30-
*
31-
* @param expirationTimestamp - if the value is not a number, data will not be cleaned
30+
* Clean Splits cache if:
31+
* - it has expired, i.e., its `lastUpdated` timestamp is older than the given `expirationTimestamp`
32+
* - hash has changes, i.e., the SDK key, flags filter criteria or flags spec version was modified
3233
*/
33-
public validateCache(settings: ISettings, expirationTimestamp?: number) {
34+
public validateCache(settings: ISettings) {
3435
// _checkExpiration
36+
const expirationTimestamp = Date.now() - DEFAULT_CACHE_EXPIRATION_IN_MILLIS;
3537
let value: string | number | null = localStorage.getItem(this.keys.buildLastUpdatedKey());
3638
if (value !== null) {
3739
value = parseInt(value, 10);
38-
if (!isNaNNumber(value) && expirationTimestamp && value < expirationTimestamp) this.clear();
40+
if (!isNaNNumber(value) && value < expirationTimestamp) this.clear();
3941
}
4042

4143
// @TODO eventually remove `_checkFilterQuery`. Cache should be cleared at the storage level, reusing same logic than PluggableStorage

src/storages/inLocalStorage/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { KeyBuilderCS, myLargeSegmentsKeyBuilder } from '../KeyBuilderCS';
77
import { isLocalStorageAvailable } from '../../utils/env/isLocalStorageAvailable';
88
import { SplitsCacheInLocal } from './SplitsCacheInLocal';
99
import { MySegmentsCacheInLocal } from './MySegmentsCacheInLocal';
10-
import { DEFAULT_CACHE_EXPIRATION_IN_MILLIS } from '../../utils/constants/browser';
1110
import { InMemoryStorageCSFactory } from '../inMemory/InMemoryStorageCS';
1211
import { LOG_PREFIX } from './constants';
1312
import { DEBUG, NONE, STORAGE_LOCALSTORAGE } from '../../utils/constants';
@@ -37,7 +36,6 @@ export function InLocalStorage(options: InLocalStorageOptions = {}): IStorageSyn
3736
const { settings, settings: { log, scheduler: { impressionsQueueSize, eventsQueueSize, }, sync: { impressionsMode } } } = params;
3837
const matchingKey = getMatching(settings.core.key);
3938
const keys = new KeyBuilderCS(prefix, matchingKey);
40-
const expirationTimestamp = Date.now() - DEFAULT_CACHE_EXPIRATION_IN_MILLIS;
4139

4240
const splits = new SplitsCacheInLocal(settings, keys);
4341
const segments = new MySegmentsCacheInLocal(log, keys);
@@ -55,7 +53,7 @@ export function InLocalStorage(options: InLocalStorageOptions = {}): IStorageSyn
5553

5654
// @TODO implement
5755
validateCache() {
58-
return splits.validateCache(settings, expirationTimestamp);
56+
return splits.validateCache(settings);
5957
},
6058

6159
destroy() { },

0 commit comments

Comments
 (0)