diff --git a/libs/providers/go-feature-flag/src/lib/controller/cache.ts b/libs/providers/go-feature-flag/src/lib/controller/cache.ts index d6c859ae7..e7bec1338 100644 --- a/libs/providers/go-feature-flag/src/lib/controller/cache.ts +++ b/libs/providers/go-feature-flag/src/lib/controller/cache.ts @@ -1,4 +1,4 @@ -import { GoFeatureFlagProviderOptions } from '../model'; +import { GoFeatureFlagProviderOptions, Cache } from '../model'; import { EvaluationContext, Logger, ResolutionDetails } from '@openfeature/server-sdk'; import { LRUCache } from 'lru-cache'; import hash from 'object-hash'; @@ -10,7 +10,7 @@ export class CacheController { // logger is the Open Feature logger to use private logger?: Logger; // cache contains the local cache used in the provider to avoid calling the relay-proxy for every evaluation - private readonly cache?: LRUCache>; + private readonly cache?: Cache; // options for this provider private readonly options: GoFeatureFlagProviderOptions; @@ -20,7 +20,7 @@ export class CacheController { this.logger = logger; const cacheSize = options.flagCacheSize !== undefined && options.flagCacheSize !== 0 ? options.flagCacheSize : 10000; - this.cache = new LRUCache({ maxSize: cacheSize, sizeCalculation: () => 1 }); + this.cache = options.cache || new LRUCache({ maxSize: cacheSize, sizeCalculation: () => 1 }); } get(flagKey: string, evaluationContext: EvaluationContext): ResolutionDetails | undefined { diff --git a/libs/providers/go-feature-flag/src/lib/model.ts b/libs/providers/go-feature-flag/src/lib/model.ts index dc33ecd91..c1f7d8bba 100644 --- a/libs/providers/go-feature-flag/src/lib/model.ts +++ b/libs/providers/go-feature-flag/src/lib/model.ts @@ -1,4 +1,4 @@ -import { ErrorCode, EvaluationContextValue } from '@openfeature/server-sdk'; +import { ErrorCode, EvaluationContextValue, ResolutionDetails } from '@openfeature/server-sdk'; export interface GOFFEvaluationContext { key: string; @@ -33,6 +33,15 @@ export interface GoFeatureFlagProxyResponse { cacheable: boolean; } +/** + * Cache is the interface used to implement an alternative cache for the provider. + */ +export interface Cache { + get: (key: string) => ResolutionDetails | undefined; + set: (key: string, value: ResolutionDetails, options?: Record) => void; + clear: () => void; +} + /** * GoFeatureFlagProviderOptions is the object containing all the provider options * when initializing the open-feature provider. @@ -47,6 +56,9 @@ export interface GoFeatureFlagProviderOptions { // Default: null apiKey?: string; + // cache (optional) set an alternative cache library. + cache?: Cache; + // disableCache (optional) set to true if you would like that every flag evaluation goes to the GO Feature Flag directly. disableCache?: boolean;