diff --git a/src/storages/inLocalStorage/SplitsCacheInLocal.ts b/src/storages/inLocalStorage/SplitsCacheInLocal.ts index dd28bd41..bd5ea343 100644 --- a/src/storages/inLocalStorage/SplitsCacheInLocal.ts +++ b/src/storages/inLocalStorage/SplitsCacheInLocal.ts @@ -7,6 +7,7 @@ import { LOG_PREFIX } from './constants'; import { ISettings } from '../../types'; import { getStorageHash } from '../KeyBuilder'; import { setToArray } from '../../utils/lang/sets'; +import { DEFAULT_CACHE_EXPIRATION_IN_MILLIS } from '../../utils/constants/browser'; /** * ISplitsCacheSync implementation that stores split definitions in browser LocalStorage. @@ -26,16 +27,17 @@ export class SplitsCacheInLocal extends AbstractSplitsCacheSync { } /** - * Clean Splits cache if its `lastUpdated` timestamp is older than the given `expirationTimestamp`, - * - * @param expirationTimestamp - if the value is not a number, data will not be cleaned + * Clean Splits cache if: + * - it has expired, i.e., its `lastUpdated` timestamp is older than the given `expirationTimestamp` + * - hash has changes, i.e., the SDK key, flags filter criteria or flags spec version was modified */ - public validateCache(settings: ISettings, expirationTimestamp?: number) { + public validateCache(settings: ISettings) { // _checkExpiration + const expirationTimestamp = Date.now() - DEFAULT_CACHE_EXPIRATION_IN_MILLIS; let value: string | number | null = localStorage.getItem(this.keys.buildLastUpdatedKey()); if (value !== null) { value = parseInt(value, 10); - if (!isNaNNumber(value) && expirationTimestamp && value < expirationTimestamp) this.clear(); + if (!isNaNNumber(value) && value < expirationTimestamp) this.clear(); } // @TODO eventually remove `_checkFilterQuery`. Cache should be cleared at the storage level, reusing same logic than PluggableStorage diff --git a/src/storages/inLocalStorage/index.ts b/src/storages/inLocalStorage/index.ts index a42e6a12..58085e17 100644 --- a/src/storages/inLocalStorage/index.ts +++ b/src/storages/inLocalStorage/index.ts @@ -7,7 +7,6 @@ import { KeyBuilderCS, myLargeSegmentsKeyBuilder } from '../KeyBuilderCS'; import { isLocalStorageAvailable } from '../../utils/env/isLocalStorageAvailable'; import { SplitsCacheInLocal } from './SplitsCacheInLocal'; import { MySegmentsCacheInLocal } from './MySegmentsCacheInLocal'; -import { DEFAULT_CACHE_EXPIRATION_IN_MILLIS } from '../../utils/constants/browser'; import { InMemoryStorageCSFactory } from '../inMemory/InMemoryStorageCS'; import { LOG_PREFIX } from './constants'; import { DEBUG, NONE, STORAGE_LOCALSTORAGE } from '../../utils/constants'; @@ -37,7 +36,6 @@ export function InLocalStorage(options: InLocalStorageOptions = {}): IStorageSyn const { settings, settings: { log, scheduler: { impressionsQueueSize, eventsQueueSize, }, sync: { impressionsMode } } } = params; const matchingKey = getMatching(settings.core.key); const keys = new KeyBuilderCS(prefix, matchingKey); - const expirationTimestamp = Date.now() - DEFAULT_CACHE_EXPIRATION_IN_MILLIS; const splits = new SplitsCacheInLocal(settings, keys); const segments = new MySegmentsCacheInLocal(log, keys); @@ -55,7 +53,7 @@ export function InLocalStorage(options: InLocalStorageOptions = {}): IStorageSyn // @TODO implement validateCache() { - return splits.validateCache(settings, expirationTimestamp); + return splits.validateCache(settings); }, destroy() { },