Skip to content

Commit 0898d84

Browse files
Adds useCustomAssignmentCache function to allow upstream clients toinject 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
1 parent 0b37ef2 commit 0898d84

File tree

5 files changed

+20
-14
lines changed

5 files changed

+20
-14
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@eppo/js-client-sdk-common",
3-
"version": "1.8.1",
3+
"version": "2.0.0",
44
"description": "Eppo SDK for client-side JavaScript applications (base for both web and react native)",
55
"main": "dist/index.js",
66
"files": [

src/assignment-cache.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export abstract class AssignmentCache<T extends Cacheable> {
5454
* The primary use case is for client-side SDKs, where the cache is only used
5555
* for a single user.
5656
*/
57-
export class NonExpiringAssignmentCache extends AssignmentCache<Map<string, string>> {
57+
export class NonExpiringInMemoryAssignmentCache extends AssignmentCache<Map<string, string>> {
5858
constructor() {
5959
super(new Map<string, string>());
6060
}
@@ -69,7 +69,7 @@ export class NonExpiringAssignmentCache extends AssignmentCache<Map<string, stri
6969
* multiple users. In this case, the cache size should be set to the maximum number
7070
* of users that can be active at the same time.
7171
*/
72-
export class LRUAssignmentCache extends AssignmentCache<LRUCache<string, string>> {
72+
export class LRUInMemoryAssignmentCache extends AssignmentCache<LRUCache<string, string>> {
7373
constructor(maxSize: number) {
7474
super(new LRUCache<string, string>({ max: maxSize }));
7575
}

src/client/eppo-client.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ describe('EppoClient E2E test', () => {
395395
});
396396

397397
it('does not log duplicate assignments', () => {
398-
client.useNonExpiringAssignmentCache();
398+
client.useNonExpiringInMemoryAssignmentCache();
399399

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

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

410410
client.getAssignment('subject-10', flagKey); // logged
411411
client.getAssignment('subject-10', flagKey); // cached
@@ -449,7 +449,7 @@ describe('EppoClient E2E test', () => {
449449
},
450450
});
451451

452-
client.useNonExpiringAssignmentCache();
452+
client.useNonExpiringInMemoryAssignmentCache();
453453

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

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

470470
storage.setEntries({
471471
[flagKey]: {
@@ -540,7 +540,7 @@ describe('EppoClient E2E test', () => {
540540
});
541541

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

545545
// original configuration version
546546
storage.setEntries({ [flagKey]: mockExperimentConfig });

src/client/eppo-client.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import * as md5 from 'md5';
33
import {
44
AssignmentCache,
55
Cacheable,
6-
LRUAssignmentCache,
7-
NonExpiringAssignmentCache,
6+
LRUInMemoryAssignmentCache,
7+
NonExpiringInMemoryAssignmentCache,
88
} from '../assignment-cache';
99
import { IAssignmentHooks } from '../assignment-hooks';
1010
import {
@@ -418,12 +418,16 @@ export default class EppoClient implements IEppoClient {
418418
this.assignmentCache = undefined;
419419
}
420420

421-
public useNonExpiringAssignmentCache() {
422-
this.assignmentCache = new NonExpiringAssignmentCache();
421+
public useNonExpiringInMemoryAssignmentCache() {
422+
this.assignmentCache = new NonExpiringInMemoryAssignmentCache();
423423
}
424424

425-
public useLRUAssignmentCache(maxSize: number) {
426-
this.assignmentCache = new LRUAssignmentCache(maxSize);
425+
public useLRUInMemoryAssignmentCache(maxSize: number) {
426+
this.assignmentCache = new LRUInMemoryAssignmentCache(maxSize);
427+
}
428+
429+
public useCustomAssignmentCache(cache: AssignmentCache<Cacheable>) {
430+
this.assignmentCache = cache;
427431
}
428432

429433
public setIsGracefulFailureMode(gracefulFailureMode: boolean) {

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { AssignmentCache } from './assignment-cache';
12
import { IAssignmentHooks } from './assignment-hooks';
23
import { IAssignmentLogger, IAssignmentEvent } from './assignment-logger';
34
import EppoClient, { IEppoClient } from './client/eppo-client';
@@ -18,4 +19,5 @@ export {
1819
HttpClient,
1920
validation,
2021
IConfigurationStore,
22+
AssignmentCache,
2123
};

0 commit comments

Comments
 (0)