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

Per-API Key Caches #74

Merged
merged 14 commits into from
Jun 7, 2024
Merged

Per-API Key Caches #74

merged 14 commits into from
Jun 7, 2024

Conversation

aarsilv
Copy link
Contributor

@aarsilv aarsilv commented May 29, 2024

Eppo Internal:
🎟️ Ticket: FF-2230 - JS SDK - local and chrome storage caches should be isolated per-API key

During development, different environments (and thus API keys) may be used on the same device. By isolating our provided persistent storages (Local Storage, Chrome Storage, and Assignment Cache) at the per-API key level, we will prevent an SDK initialized for one environment from loading a cached configuration from a different environment.

@aarsilv aarsilv changed the base branch from main to aaron/ff-2218/cache-outcomes May 29, 2024 03:15
@@ -550,7 +550,6 @@ describe('initialization options', () => {
let client = await init({
apiKey,
baseUrl,
assignmentLogger: mockLogger,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Didn't need this 🪓

@@ -2,8 +2,14 @@ import { AssignmentCache } from '@eppo/js-client-sdk-common';

import { hasWindowLocalStorage } from './configuration-factory';

// noinspection JSUnusedGlobalSymbols (methods are used by common repository)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

stop Webstorm for yelling at me for unused methods

beforeEach(() => {
window.localStorage.clear();
});

describe('get and set', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

opted to remove the nested describe()

@@ -179,9 +179,15 @@ export async function init(config: IClientConfig): Promise<IEppoClient> {
EppoJSClient.instance.stopPolling();
// Set up assignment logger and cache
EppoJSClient.instance.setLogger(config.assignmentLogger);

// Note that we use the first 8 characters of the API key to create per-API key persistent storages
const storageKeySuffix = config.apiKey.replace(/\W/g, '').substring(0, 8);
Copy link
Member

Choose a reason for hiding this comment

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

👍

Comment on lines +188 to +190
EppoJSClient.instance.useCustomAssignmentCache(
new LocalStorageAssignmentCache(storageKeySuffix),
);
Copy link
Member

Choose a reason for hiding this comment

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

@@ -49,4 +49,69 @@ describe('LocalStorageAssignmentCache', () => {
}),
).toEqual(false); // this key has not been logged
});

it('can have independent caches', () => {
Copy link
Member

Choose a reason for hiding this comment

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

👨‍🍳

LOCAL_STORAGE_KEY = 'EPPO_LOCAL_STORAGE_ASSIGNMENT_CACHE';
private readonly localStorageKey: string;

public constructor(storageKeySuffix?: string) {
Copy link
Member

Choose a reason for hiding this comment

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

did you consider making this a required parameter?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thought about it. I suppose I should since we never really use it without one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Making this required caught a place I forgot to pass it through! Excellent suggestion!

Copy link
Collaborator

@greghuels greghuels left a comment

Choose a reason for hiding this comment

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

I love the tests. Approved, with the caveat that I'm also looking at this repo for the first time.

@aarsilv aarsilv force-pushed the aaron/ff-2218/cache-outcomes branch from 17d61b8 to 011f8c6 Compare June 6, 2024 20:44
@aarsilv aarsilv assigned aarsilv and unassigned leoromanovsky Jun 6, 2024
Base automatically changed from aaron/ff-2218/cache-outcomes to main June 6, 2024 22:12
@aarsilv aarsilv merged commit 025f80d into main Jun 7, 2024
2 checks passed
@aarsilv aarsilv deleted the aaron/ff-2230/per-api-key-caches branch June 7, 2024 15:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants