Skip to content

Commit 87fbc4f

Browse files
expirationDays configuration
1 parent 9b8d36a commit 87fbc4f

File tree

6 files changed

+45
-14
lines changed

6 files changed

+45
-14
lines changed

src/storages/dataLoader.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { PreloadedData } from '../types';
2-
import { DEFAULT_CACHE_EXPIRATION_IN_MILLIS } from '../utils/constants/browser';
32
import { DataLoader, ISegmentsCacheSync, ISplitsCacheSync } from './types';
43

4+
// This value might be eventually set via a config parameter
5+
const DEFAULT_CACHE_EXPIRATION_IN_MILLIS = 864000000; // 10 days
6+
57
/**
68
* Factory of client-side storage loader
79
*

src/storages/inLocalStorage/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export function InLocalStorage(options: SplitIO.InLocalStorageOptions = {}): ISt
5050
uniqueKeys: impressionsMode === NONE ? new UniqueKeysCacheInMemoryCS() : undefined,
5151

5252
validateCache() {
53-
return validateCache(settings, keys, splits, segments, largeSegments);
53+
return validateCache(options, settings, keys, splits, segments, largeSegments);
5454
},
5555

5656
destroy() { },

src/storages/inLocalStorage/validateCache.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
import { ISettings } from '../../types';
2-
import { DEFAULT_CACHE_EXPIRATION_IN_MILLIS } from '../../utils/constants/browser';
3-
import { isNaNNumber } from '../../utils/lang';
2+
import { isFiniteNumber, isNaNNumber } from '../../utils/lang';
43
import { getStorageHash } from '../KeyBuilder';
54
import { LOG_PREFIX } from './constants';
65
import type { SplitsCacheInLocal } from './SplitsCacheInLocal';
76
import type { MySegmentsCacheInLocal } from './MySegmentsCacheInLocal';
87
import { KeyBuilderCS } from '../KeyBuilderCS';
8+
import SplitIO from '../../../types/splitio';
99

10-
function validateExpiration(settings: ISettings, keys: KeyBuilderCS) {
10+
// milliseconds in a day
11+
const DEFAULT_CACHE_EXPIRATION_IN_DAYS = 10;
12+
const MILLIS_IN_A_DAY = 86400000;
13+
14+
function validateExpiration(options: SplitIO.InLocalStorageOptions, settings: ISettings, keys: KeyBuilderCS) {
1115
const { log } = settings;
1216

1317
// Check expiration
14-
const expirationTimestamp = Date.now() - DEFAULT_CACHE_EXPIRATION_IN_MILLIS;
18+
const expirationTimestamp = Date.now() - MILLIS_IN_A_DAY * (isFiniteNumber(options.expirationDays) && options.expirationDays >= 1 ? options.expirationDays : DEFAULT_CACHE_EXPIRATION_IN_DAYS);
1519
let value: string | number | null = localStorage.getItem(keys.buildLastUpdatedKey());
1620
if (value !== null) {
1721
value = parseInt(value, 10);
18-
if (!isNaNNumber(value) && value < expirationTimestamp) return true;
22+
if (!isNaNNumber(value) && value < expirationTimestamp) {
23+
log.info(LOG_PREFIX + 'Cache expired. Cleaning up cache');
24+
return true;
25+
}
1926
}
2027

2128
// Check hash
@@ -24,7 +31,7 @@ function validateExpiration(settings: ISettings, keys: KeyBuilderCS) {
2431
const currentStorageHash = getStorageHash(settings);
2532

2633
if (storageHash !== currentStorageHash) {
27-
log.info(LOG_PREFIX + 'SDK key, flags filter criteria or flags spec version was modified. Updating cache');
34+
log.info(LOG_PREFIX + 'SDK key, flags filter criteria or flags spec version was modified. Cleaning up cache');
2835
try {
2936
localStorage.setItem(storageHashKey, currentStorageHash);
3037
} catch (e) {
@@ -39,9 +46,9 @@ function validateExpiration(settings: ISettings, keys: KeyBuilderCS) {
3946
* - it has expired, i.e., its `lastUpdated` timestamp is older than the given `expirationTimestamp`
4047
* - hash has changed, i.e., the SDK key, flags filter criteria or flags spec version was modified
4148
*/
42-
export function validateCache(settings: ISettings, keys: KeyBuilderCS, splits: SplitsCacheInLocal, segments: MySegmentsCacheInLocal, largeSegments: MySegmentsCacheInLocal): boolean {
49+
export function validateCache(options: SplitIO.InLocalStorageOptions, settings: ISettings, keys: KeyBuilderCS, splits: SplitsCacheInLocal, segments: MySegmentsCacheInLocal, largeSegments: MySegmentsCacheInLocal): boolean {
4350

44-
if (validateExpiration(settings, keys)) {
51+
if (validateExpiration(options, settings, keys)) {
4552
splits.clear();
4653
segments.clear();
4754
largeSegments.clear();

src/utils/constants/browser.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/utils/lang/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export function isBoolean(val: any): boolean {
120120
* Unlike `Number.isFinite`, it also tests Number object instances.
121121
* Unlike global `isFinite`, it returns false if the value is not a number or Number object instance.
122122
*/
123-
export function isFiniteNumber(val: any): boolean {
123+
export function isFiniteNumber(val: any): val is number {
124124
if (val instanceof Number) val = val.valueOf();
125125
return typeof val === 'number' ?
126126
Number.isFinite ? Number.isFinite(val) : isFinite(val) :

types/splitio.d.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,18 @@ declare namespace SplitIO {
906906
* @defaultValue `'SPLITIO'`
907907
*/
908908
prefix?: string;
909+
/**
910+
* Number of days before cached data expires if it was not updated. If cache expires, it is cleared on initialization.
911+
*
912+
* @defaultValue `10`
913+
*/
914+
expirationDays?: number;
915+
/**
916+
* Optional settings to clear the cache. If set to `true`, the SDK clears the cached data on initialization, unless the cache was cleared within the last 24 hours.
917+
*
918+
* @defaultValue `false`
919+
*/
920+
clearOnInit?: boolean;
909921
}
910922
/**
911923
* Storage for asynchronous (consumer) SDK.
@@ -1229,11 +1241,23 @@ declare namespace SplitIO {
12291241
*/
12301242
type?: BrowserStorage;
12311243
/**
1232-
* Optional prefix to prevent any kind of data collision between SDK versions.
1244+
* Optional prefix to prevent any kind of data collision between SDK versions when using 'LOCALSTORAGE'.
12331245
*
12341246
* @defaultValue `'SPLITIO'`
12351247
*/
12361248
prefix?: string;
1249+
/**
1250+
* Optional settings for the 'LOCALSTORAGE' storage type. It specifies the number of days before cached data expires if it was not updated. If cache expires, it is cleared on initialization.
1251+
*
1252+
* @defaultValue `10`
1253+
*/
1254+
expirationDays?: number;
1255+
/**
1256+
* Optional settings for the 'LOCALSTORAGE' storage type. If set to `true`, the SDK clears the cached data on initialization, unless the cache was cleared within the last 24 hours.
1257+
*
1258+
* @defaultValue `false`
1259+
*/
1260+
clearOnInit?: boolean;
12371261
};
12381262
}
12391263
/**

0 commit comments

Comments
 (0)