Skip to content
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

feat: Add cacheTtlMs option #760

Merged
merged 14 commits into from
Jan 30, 2025
3 changes: 0 additions & 3 deletions packages/shared/akamai-edgeworker-sdk/src/api/LDClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class LDClient extends LDClientImpl {
defaultValue: LDFlagValue,
callback?: (err: any, res: LDFlagValue) => void,
): Promise<LDFlagValue> {
await this._cacheableStoreProvider.prefetchPayloadFromOriginStore();
return super.variation(key, context, defaultValue, callback);
}

Expand All @@ -56,7 +55,6 @@ class LDClient extends LDClientImpl {
defaultValue: LDFlagValue,
callback?: (err: any, res: LDEvaluationDetail) => void,
): Promise<LDEvaluationDetail> {
await this._cacheableStoreProvider.prefetchPayloadFromOriginStore();
return super.variationDetail(key, context, defaultValue, callback);
}

Expand All @@ -65,7 +63,6 @@ class LDClient extends LDClientImpl {
options?: LDFlagsStateOptions,
callback?: (err: Error | null, res: LDFlagsState) => void,
): Promise<LDFlagsState> {
await this._cacheableStoreProvider.prefetchPayloadFromOriginStore();
return super.allFlagsState(context, options, callback);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

/**
* Wraps around an edge provider to cache a copy of the sdk payload locally an explicit request is made to refetch data from the origin.
* The wrapper is neccessary to ensure that we dont make redundant sub-requests from Akamai to fetch an entire environment payload.
* The wrapper is necessary to ensure that we don't make redundant sub-requests from Akamai to fetch an entire environment payload.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should maybe note that, at the time of writing, there is a 4 request sub-limit.

I think we also want to update the docs to indicate using the SDK uses one of their sub-requests. In case they already need to make their own sub requests for other reasons.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think that is a fair point. I still have some loose ends I want to follow up with on this PR, so this isn't getting merged any time soon I don't think.

I'll make sure to get the docs updated and much more explicit about this one we have all this clarified.

*/
export default class CacheableStoreProvider implements EdgeProvider {
cache: string | null | undefined;

constructor(

Check failure on line 10 in packages/shared/akamai-edgeworker-sdk/src/featureStore/cacheableStoreProvider.ts

View workflow job for this annotation

GitHub Actions / build-test-akamai-edgeworker-sdk

Replace `⏎····private·readonly·_edgeProvider:·EdgeProvider,⏎··` with `private·readonly·_edgeProvider:·EdgeProvider`
private readonly _edgeProvider: EdgeProvider,
private readonly _rootKey: string,
) {}

/**
Expand All @@ -24,16 +23,4 @@

return this.cache;
}

/**
* Invalidates cache and fetch environment payload data from origin. The result of this data is cached in memory.
* You should only call this function within a feature store to pre-fetch and cache payload data in environments
* where its expensive to make multiple outbound requests to the origin
* @param rootKey
* @returns
*/
async prefetchPayloadFromOriginStore(rootKey?: string): Promise<string | null | undefined> {
this.cache = undefined; // clear the cache so that new data can be fetched from the origin
return this.get(rootKey || this._rootKey);
}
}
5 changes: 1 addition & 4 deletions packages/shared/akamai-edgeworker-sdk/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BasicLogger, LDOptions as LDOptionsCommon } from '@launchdarkly/js-server-sdk-common';

import LDClient from './api/LDClient';
import { buildRootKey, EdgeFeatureStore, EdgeProvider } from './featureStore';

Check failure on line 4 in packages/shared/akamai-edgeworker-sdk/src/index.ts

View workflow job for this annotation

GitHub Actions / build-test-akamai-edgeworker-sdk

'buildRootKey' is defined but never used. Allowed unused vars must match /^__/u
import CacheableStoreProvider from './featureStore/cacheableStoreProvider';
import EdgePlatform from './platform';
import createPlatformInfo from './platform/info';
Expand Down Expand Up @@ -37,10 +37,7 @@

const logger = options.logger ?? BasicLogger.get();

const cachableStoreProvider = new CacheableStoreProvider(
featureStoreProvider,
buildRootKey(sdkKey),
);
const cachableStoreProvider = new CacheableStoreProvider(featureStoreProvider);
const featureStore = new EdgeFeatureStore(cachableStoreProvider, sdkKey, 'Akamai', logger);

const ldOptions: LDOptionsCommon = {
Expand Down
Loading