Skip to content

feat(gofeatureflag): added cache option #1284

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 26, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions libs/providers/go-feature-flag/src/lib/controller/cache.ts
Original file line number Diff line number Diff line change
@@ -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<string, ResolutionDetails<any>>;
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<any> | undefined {
14 changes: 13 additions & 1 deletion libs/providers/go-feature-flag/src/lib/model.ts
Original file line number Diff line number Diff line change
@@ -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<T> {
cacheable: boolean;
}

/**
* Cache is the interface used to implement an alternative cache for the provider.
*/
export interface Cache {
get: (key: string) => ResolutionDetails<any> | undefined;
set: (key: string, value: ResolutionDetails<any>, options?: Record<string, any>) => 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;