Skip to content

Commit

Permalink
Adds useCustomAssignmentCache function to allow upstream clients to…
Browse files Browse the repository at this point in the history
…inject their own implementation (FF-839) (#33)

* Adds `useCustomAssignmentCache` function to allow upstream clients to inject their own implementation (FF-839)

* bump to v2.0.0
  • Loading branch information
leoromanovsky committed Dec 18, 2023
1 parent 0b37ef2 commit 0898d84
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@eppo/js-client-sdk-common",
"version": "1.8.1",
"version": "2.0.0",
"description": "Eppo SDK for client-side JavaScript applications (base for both web and react native)",
"main": "dist/index.js",
"files": [
Expand Down
4 changes: 2 additions & 2 deletions src/assignment-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export abstract class AssignmentCache<T extends Cacheable> {
* The primary use case is for client-side SDKs, where the cache is only used
* for a single user.
*/
export class NonExpiringAssignmentCache extends AssignmentCache<Map<string, string>> {
export class NonExpiringInMemoryAssignmentCache extends AssignmentCache<Map<string, string>> {
constructor() {
super(new Map<string, string>());
}
Expand All @@ -69,7 +69,7 @@ export class NonExpiringAssignmentCache extends AssignmentCache<Map<string, stri
* multiple users. In this case, the cache size should be set to the maximum number
* of users that can be active at the same time.
*/
export class LRUAssignmentCache extends AssignmentCache<LRUCache<string, string>> {
export class LRUInMemoryAssignmentCache extends AssignmentCache<LRUCache<string, string>> {
constructor(maxSize: number) {
super(new LRUCache<string, string>({ max: maxSize }));
}
Expand Down
10 changes: 5 additions & 5 deletions src/client/eppo-client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ describe('EppoClient E2E test', () => {
});

it('does not log duplicate assignments', () => {
client.useNonExpiringAssignmentCache();
client.useNonExpiringInMemoryAssignmentCache();

client.getAssignment('subject-10', flagKey);
client.getAssignment('subject-10', flagKey);
Expand All @@ -405,7 +405,7 @@ describe('EppoClient E2E test', () => {
});

it('logs assignment again after the lru cache is full', () => {
client.useLRUAssignmentCache(2);
client.useLRUInMemoryAssignmentCache(2);

client.getAssignment('subject-10', flagKey); // logged
client.getAssignment('subject-10', flagKey); // cached
Expand Down Expand Up @@ -449,7 +449,7 @@ describe('EppoClient E2E test', () => {
},
});

client.useNonExpiringAssignmentCache();
client.useNonExpiringInMemoryAssignmentCache();

client.getAssignment('subject-10', flagKey);
client.getAssignment('subject-10', flagKey);
Expand All @@ -465,7 +465,7 @@ describe('EppoClient E2E test', () => {
});

it('logs twice for the same flag when rollout increases/flag changes', () => {
client.useNonExpiringAssignmentCache();
client.useNonExpiringInMemoryAssignmentCache();

storage.setEntries({
[flagKey]: {
Expand Down Expand Up @@ -540,7 +540,7 @@ describe('EppoClient E2E test', () => {
});

it('logs the same subject/flag/variation after two changes', () => {
client.useNonExpiringAssignmentCache();
client.useNonExpiringInMemoryAssignmentCache();

// original configuration version
storage.setEntries({ [flagKey]: mockExperimentConfig });
Expand Down
16 changes: 10 additions & 6 deletions src/client/eppo-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import * as md5 from 'md5';
import {
AssignmentCache,
Cacheable,
LRUAssignmentCache,
NonExpiringAssignmentCache,
LRUInMemoryAssignmentCache,
NonExpiringInMemoryAssignmentCache,
} from '../assignment-cache';
import { IAssignmentHooks } from '../assignment-hooks';
import {
Expand Down Expand Up @@ -418,12 +418,16 @@ export default class EppoClient implements IEppoClient {
this.assignmentCache = undefined;
}

public useNonExpiringAssignmentCache() {
this.assignmentCache = new NonExpiringAssignmentCache();
public useNonExpiringInMemoryAssignmentCache() {
this.assignmentCache = new NonExpiringInMemoryAssignmentCache();
}

public useLRUAssignmentCache(maxSize: number) {
this.assignmentCache = new LRUAssignmentCache(maxSize);
public useLRUInMemoryAssignmentCache(maxSize: number) {
this.assignmentCache = new LRUInMemoryAssignmentCache(maxSize);
}

public useCustomAssignmentCache(cache: AssignmentCache<Cacheable>) {
this.assignmentCache = cache;
}

public setIsGracefulFailureMode(gracefulFailureMode: boolean) {
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AssignmentCache } from './assignment-cache';
import { IAssignmentHooks } from './assignment-hooks';
import { IAssignmentLogger, IAssignmentEvent } from './assignment-logger';
import EppoClient, { IEppoClient } from './client/eppo-client';
Expand All @@ -18,4 +19,5 @@ export {
HttpClient,
validation,
IConfigurationStore,
AssignmentCache,
};

0 comments on commit 0898d84

Please sign in to comment.