Skip to content

Commit

Permalink
Merge pull request #251 from blackjid/add_load_stale_cache_option
Browse files Browse the repository at this point in the history
add loadStale cache option to load already expired cache data
  • Loading branch information
kyle-ssg authored Sep 17, 2024
2 parents f2c2964 + abd9699 commit 2f4bbbb
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 6 deletions.
10 changes: 7 additions & 3 deletions flagsmith-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ const Flagsmith = class {
traits:ITraits|null= null
dtrum= null
withTraits?: ITraits|null= null
cacheOptions = {ttl:0, skipAPI: false}
cacheOptions = {ttl:0, skipAPI: false, loadStale: false}
async init(config: IInitConfig) {
try {
const {
Expand Down Expand Up @@ -306,7 +306,7 @@ const Flagsmith = class {
this.identity = identity;
this.withTraits = traits;
this.enableLogs = enableLogs || false;
this.cacheOptions = cacheOptions ? { skipAPI: !!cacheOptions.skipAPI, ttl: cacheOptions.ttl || 0 } : this.cacheOptions;
this.cacheOptions = cacheOptions ? { skipAPI: !!cacheOptions.skipAPI, ttl: cacheOptions.ttl || 0, loadStale: !!cacheOptions.loadStale } : this.cacheOptions;
if (!this.cacheOptions.ttl && this.cacheOptions.skipAPI) {
console.warn("Flagsmith: you have set a cache ttl of 0 and are skipping API calls, this means the API will not be hit unless you clear local storage.")
}
Expand Down Expand Up @@ -412,10 +412,14 @@ const Flagsmith = class {
}
if (this.cacheOptions.ttl) {
if (!json.ts || (new Date().valueOf() - json.ts > this.cacheOptions.ttl)) {
if (json.ts) {
if (json.ts && !this.cacheOptions.loadStale) {
this.log("Ignoring cache, timestamp is too old ts:" + json.ts + " ttl: " + this.cacheOptions.ttl + " time elapsed since cache: " + (new Date().valueOf()-json.ts)+"ms")
setState = false;
}
else if (json.ts && this.cacheOptions.loadStale) {
this.log("Loading stale cache, timestamp ts:" + json.ts + " ttl: " + this.cacheOptions.ttl + " time elapsed since cache: " + (new Date().valueOf()-json.ts)+"ms")
setState = true;
}
}
}
if (setState) {
Expand Down
2 changes: 1 addition & 1 deletion lib/flagsmith-es/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "flagsmith-es",
"version": "4.1.2",
"version": "4.1.3",
"description": "Feature flagging to support continuous development. This is an esm equivalent of the standard flagsmith npm module.",
"main": "./index.js",
"type": "module",
Expand Down
2 changes: 1 addition & 1 deletion lib/flagsmith/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "flagsmith",
"version": "4.1.2",
"version": "4.1.3",
"description": "Feature flagging to support continuous development",
"main": "./index.js",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion lib/react-native-flagsmith/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-flagsmith",
"version": "4.1.2",
"version": "4.1.3",
"description": "Feature flagging to support continuous development",
"main": "./index.js",
"repository": {
Expand Down
18 changes: 18 additions & 0 deletions test/cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,24 @@ describe('Cache', () => {
...defaultState,
});
});
test('should not ignore cache with expired ttl and loadStale is set', async () => {
const onChange = jest.fn();
const { flagsmith, initConfig, AsyncStorage, mockFetch } = getFlagsmith({
cacheFlags: true,
onChange,
cacheOptions: { ttl: 1, loadStale: true },
});
await AsyncStorage.setItem('BULLET_TRAIN_DB', JSON.stringify({
...defaultStateAlt,
ts: new Date().valueOf() - 100,
}));
await flagsmith.init(initConfig);
expect(onChange).toHaveBeenCalledTimes(1);
expect(mockFetch).toHaveBeenCalledTimes(1);
expect(getStateToCheck(flagsmith.getState())).toEqual({
...defaultStateAlt,
});
});
test('should not ignore cache with valid ttl', async () => {
const onChange = jest.fn();
const { flagsmith, initConfig, AsyncStorage, mockFetch } = getFlagsmith({
Expand Down
2 changes: 2 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export interface IState<F extends string = string, T extends string = string> {
declare type ICacheOptions = {
ttl?: number;
skipAPI?: boolean;
loadStale?: boolean;
};

export declare type IDatadogRum = {
Expand Down Expand Up @@ -233,6 +234,7 @@ export interface IFlagsmith<F extends string = string, T extends string = string
cacheOptions: {
ttl: number;
skipAPI: boolean;
loadStale: boolean;
};
/**
* Used internally, this is the api provided in flagsmith.init, defaults to our production API
Expand Down

0 comments on commit 2f4bbbb

Please sign in to comment.