Skip to content

Commit

Permalink
Move expirationTimestamp logic inside validateCache method
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilianoSanchez committed Dec 18, 2024
1 parent ffd724d commit e13f819
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
12 changes: 7 additions & 5 deletions src/storages/inLocalStorage/SplitsCacheInLocal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
4 changes: 1 addition & 3 deletions src/storages/inLocalStorage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand All @@ -55,7 +53,7 @@ export function InLocalStorage(options: InLocalStorageOptions = {}): IStorageSyn

// @TODO implement
validateCache() {
return splits.validateCache(settings, expirationTimestamp);
return splits.validateCache(settings);
},

destroy() { },
Expand Down

0 comments on commit e13f819

Please sign in to comment.