Skip to content

Commit 7083655

Browse files
authored
feat(gofeatureflag): added cache option (#1284)
Signed-off-by: Simas Usas <simas.usas@vinted.com>
1 parent df1ec47 commit 7083655

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

libs/providers/go-feature-flag/src/lib/controller/cache.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { GoFeatureFlagProviderOptions } from '../model';
1+
import { GoFeatureFlagProviderOptions, Cache } from '../model';
22
import { EvaluationContext, Logger, ResolutionDetails } from '@openfeature/server-sdk';
33
import { LRUCache } from 'lru-cache';
44
import hash from 'object-hash';
@@ -10,7 +10,7 @@ export class CacheController {
1010
// logger is the Open Feature logger to use
1111
private logger?: Logger;
1212
// cache contains the local cache used in the provider to avoid calling the relay-proxy for every evaluation
13-
private readonly cache?: LRUCache<string, ResolutionDetails<any>>;
13+
private readonly cache?: Cache;
1414
// options for this provider
1515
private readonly options: GoFeatureFlagProviderOptions;
1616

@@ -20,7 +20,7 @@ export class CacheController {
2020
this.logger = logger;
2121
const cacheSize =
2222
options.flagCacheSize !== undefined && options.flagCacheSize !== 0 ? options.flagCacheSize : 10000;
23-
this.cache = new LRUCache({ maxSize: cacheSize, sizeCalculation: () => 1 });
23+
this.cache = options.cache || new LRUCache({ maxSize: cacheSize, sizeCalculation: () => 1 });
2424
}
2525

2626
get(flagKey: string, evaluationContext: EvaluationContext): ResolutionDetails<any> | undefined {

libs/providers/go-feature-flag/src/lib/model.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ErrorCode, EvaluationContextValue } from '@openfeature/server-sdk';
1+
import { ErrorCode, EvaluationContextValue, ResolutionDetails } from '@openfeature/server-sdk';
22

33
export interface GOFFEvaluationContext {
44
key: string;
@@ -33,6 +33,15 @@ export interface GoFeatureFlagProxyResponse<T> {
3333
cacheable: boolean;
3434
}
3535

36+
/**
37+
* Cache is the interface used to implement an alternative cache for the provider.
38+
*/
39+
export interface Cache {
40+
get: (key: string) => ResolutionDetails<any> | undefined;
41+
set: (key: string, value: ResolutionDetails<any>, options?: Record<string, any>) => void;
42+
clear: () => void;
43+
}
44+
3645
/**
3746
* GoFeatureFlagProviderOptions is the object containing all the provider options
3847
* when initializing the open-feature provider.
@@ -47,6 +56,9 @@ export interface GoFeatureFlagProviderOptions {
4756
// Default: null
4857
apiKey?: string;
4958

59+
// cache (optional) set an alternative cache library.
60+
cache?: Cache;
61+
5062
// disableCache (optional) set to true if you would like that every flag evaluation goes to the GO Feature Flag directly.
5163
disableCache?: boolean;
5264

0 commit comments

Comments
 (0)