Skip to content

Commit

Permalink
Refactor validateCache function
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilianoSanchez committed Dec 18, 2024
1 parent 6070b54 commit 6605bfc
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/storages/inLocalStorage/validateCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,42 @@ import { LOG_PREFIX } from './constants';
import type { SplitsCacheInLocal } from './SplitsCacheInLocal';
import { KeyBuilderCS } from '../KeyBuilderCS';

/**
* Clean cache if:
* - it has expired, i.e., its `lastUpdated` timestamp is older than the given `expirationTimestamp`
* - hash has changed, i.e., the SDK key, flags filter criteria or flags spec version was modified
*/
export function validateCache(settings: ISettings, keys: KeyBuilderCS, splits: SplitsCacheInLocal): boolean {
function validateExpiration(settings: ISettings, keys: KeyBuilderCS) {
const { log } = settings;

// Check expiration and clear cache if needed
// Check expiration
const expirationTimestamp = Date.now() - DEFAULT_CACHE_EXPIRATION_IN_MILLIS;
let value: string | number | null = localStorage.getItem(keys.buildLastUpdatedKey());
if (value !== null) {
value = parseInt(value, 10);
if (!isNaNNumber(value) && value < expirationTimestamp) splits.clear();
if (!isNaNNumber(value) && value < expirationTimestamp) return true;
}

// Check hash and clear cache if needed
// Check hash
const storageHashKey = keys.buildHashKey();
const storageHash = localStorage.getItem(storageHashKey);
const currentStorageHash = getStorageHash(settings);

if (storageHash !== currentStorageHash) {
log.info(LOG_PREFIX + 'SDK key, flags filter criteria or flags spec version was modified. Updating cache');
try {
if (splits.getChangeNumber() > -1) splits.clear();

localStorage.setItem(storageHashKey, currentStorageHash);
} catch (e) {
log.error(LOG_PREFIX + e);
}
return true;
}
}

/**
* Clean cache if:
* - it has expired, i.e., its `lastUpdated` timestamp is older than the given `expirationTimestamp`
* - hash has changed, i.e., the SDK key, flags filter criteria or flags spec version was modified
*/
export function validateCache(settings: ISettings, keys: KeyBuilderCS, splits: SplitsCacheInLocal): boolean {

if (validateExpiration(settings, keys)) {
splits.clear();
}

// Check if the cache is ready
Expand Down

0 comments on commit 6605bfc

Please sign in to comment.