Skip to content

Commit 90429c1

Browse files
Reorganized caches into their own files
1 parent ffc0c59 commit 90429c1

File tree

7 files changed

+59
-60
lines changed

7 files changed

+59
-60
lines changed

src/cache/abstract-assignment-cache.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {
22
assignmentCacheKeyToString,
33
assignmentCacheValueToString,
4-
NonExpiringInMemoryAssignmentCache,
54
} from './abstract-assignment-cache';
5+
import { NonExpiringInMemoryAssignmentCache } from './non-expiring-in-memory-cache-assignment';
66

77
describe('NonExpiringInMemoryAssignmentCache', () => {
88
it('read and write variation entries', () => {

src/cache/abstract-assignment-cache.ts

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import { getMD5Hash } from '../obfuscation';
22

3-
import {ExpiringLRUCache, LRUCache} from './lru-cache';
4-
import {max} from "lodash";
5-
63
/**
74
* Assignment cache keys are only on the subject and flag level, while the entire value is used
85
* for uniqueness checking. This way that if an assigned variation or bandit action changes for a
@@ -81,33 +78,3 @@ export abstract class AbstractAssignmentCache<T extends Map<string, string>>
8178
return this.delegate.entries();
8279
}
8380
}
84-
85-
/**
86-
* A cache that never expires.
87-
*
88-
* The primary use case is for client-side SDKs, where the cache is only used
89-
* for a single user.
90-
*/
91-
export class NonExpiringInMemoryAssignmentCache extends AbstractAssignmentCache<
92-
Map<string, string>
93-
> {
94-
constructor(store = new Map<string, string>()) {
95-
super(store);
96-
}
97-
}
98-
99-
/**
100-
* A cache that uses the LRU algorithm to evict the least recently used items.
101-
*
102-
* It is used to limit the size of the cache.
103-
*
104-
* The primary use case is for server-side SDKs, where the cache is shared across
105-
* multiple users. In this case, the cache size should be set to the maximum number
106-
* of users that can be active at the same time.
107-
* @param {number} maxSize - Maximum cache size
108-
*/
109-
export class LRUInMemoryAssignmentCache extends AbstractAssignmentCache<LRUCache> {
110-
constructor(maxSize: number) {
111-
super(new LRUCache(maxSize));
112-
}
113-
}

src/cache/lru-cache.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ export class LRUCache implements Map<string, string> {
9797
return this;
9898
}
9999
}
100+
100101
/**
101102
* Variation of LRUCache that expires after set time in milliseconds
102103
* @param {number} maxSize - Maximum cache size
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { AbstractAssignmentCache } from './abstract-assignment-cache';
2+
import { LRUCache } from './lru-cache';
3+
4+
/**
5+
* A cache that uses the LRU algorithm to evict the least recently used items.
6+
*
7+
* It is used to limit the size of the cache.
8+
*
9+
* The primary use case is for server-side SDKs, where the cache is shared across
10+
* multiple users. In this case, the cache size should be set to the maximum number
11+
* of users that can be active at the same time.
12+
* @param {number} maxSize - Maximum cache size
13+
*/
14+
export class LRUInMemoryAssignmentCache extends AbstractAssignmentCache<LRUCache> {
15+
constructor(maxSize: number) {
16+
super(new LRUCache(maxSize));
17+
}
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { AbstractAssignmentCache } from './abstract-assignment-cache';
2+
3+
/**
4+
* A cache that never expires.
5+
*
6+
* The primary use case is for client-side SDKs, where the cache is only used
7+
* for a single user.
8+
*/
9+
export class NonExpiringInMemoryAssignmentCache extends AbstractAssignmentCache<
10+
Map<string, string>
11+
> {
12+
constructor(store = new Map<string, string>()) {
13+
super(store);
14+
}
15+
}

src/client/eppo-client.ts

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ import { logger } from '../application-logger';
33
import { IAssignmentEvent, IAssignmentLogger } from '../assignment-logger';
44
import { BanditEvaluator } from '../bandit-evaluator';
55
import { IBanditEvent, IBanditLogger } from '../bandit-logger';
6-
import {
7-
AssignmentCache,
8-
LRUInMemoryAssignmentCache,
9-
NonExpiringInMemoryAssignmentCache,
10-
} from '../cache/abstract-assignment-cache';
6+
import { AssignmentCache } from '../cache/abstract-assignment-cache';
7+
import { LRUInMemoryAssignmentCache } from '../cache/lru-in-memory-assignment-cache';
8+
import { NonExpiringInMemoryAssignmentCache } from '../cache/non-expiring-in-memory-cache-assignment';
119
import ConfigurationRequestor from '../configuration-requestor';
1210
import { IConfigurationStore } from '../configuration-store/configuration-store';
1311
import {
@@ -254,25 +252,6 @@ export default class EppoClient {
254252
return this.getBooleanAssignment(flagKey, subjectKey, subjectAttributes, defaultValue);
255253
}
256254

257-
/**
258-
* Maps a subject to a boolean variation for a given experiment.
259-
*
260-
* @param flagKey feature flag identifier
261-
* @param subjectKey an identifier of the experiment subject, for example a user ID.
262-
* @param subjectAttributes optional attributes associated with the subject, for example name and email.
263-
* @param defaultValue default value to return if the subject is not part of the experiment sample
264-
* @returns a boolean variation value if the subject is part of the experiment sample, otherwise the default value
265-
*/
266-
public getBooleanAssignment(
267-
flagKey: string,
268-
subjectKey: string,
269-
subjectAttributes: Attributes,
270-
defaultValue: boolean,
271-
): boolean {
272-
return this.getBooleanAssignmentDetails(flagKey, subjectKey, subjectAttributes, defaultValue)
273-
.variation;
274-
}
275-
276255
/**
277256
* Maps a subject to a boolean variation for a given experiment and provides additional details about the
278257
* variation assigned and the reason for the assignment.
@@ -657,6 +636,25 @@ export default class EppoClient {
657636
return result;
658637
}
659638

639+
/**
640+
* Maps a subject to a boolean variation for a given experiment.
641+
*
642+
* @param flagKey feature flag identifier
643+
* @param subjectKey an identifier of the experiment subject, for example a user ID.
644+
* @param subjectAttributes optional attributes associated with the subject, for example name and email.
645+
* @param defaultValue default value to return if the subject is not part of the experiment sample
646+
* @returns a boolean variation value if the subject is part of the experiment sample, otherwise the default value
647+
*/
648+
public getBooleanAssignment(
649+
flagKey: string,
650+
subjectKey: string,
651+
subjectAttributes: Attributes,
652+
defaultValue: boolean,
653+
): boolean {
654+
return this.getBooleanAssignmentDetails(flagKey, subjectKey, subjectAttributes, defaultValue)
655+
.variation;
656+
}
657+
660658
private ensureActionsWithContextualAttributes(
661659
actions: BanditActions,
662660
): Record<string, ContextAttributes> {

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ import { IBanditLogger, IBanditEvent } from './bandit-logger';
66
import {
77
AbstractAssignmentCache,
88
AssignmentCache,
9-
NonExpiringInMemoryAssignmentCache,
10-
LRUInMemoryAssignmentCache,
119
AsyncMap,
1210
AssignmentCacheKey,
1311
AssignmentCacheValue,
1412
AssignmentCacheEntry,
1513
assignmentCacheKeyToString,
1614
assignmentCacheValueToString,
1715
} from './cache/abstract-assignment-cache';
16+
import { LRUInMemoryAssignmentCache } from './cache/lru-in-memory-assignment-cache';
17+
import { NonExpiringInMemoryAssignmentCache } from './cache/non-expiring-in-memory-cache-assignment';
1818
import EppoClient, {
1919
FlagConfigurationRequestParameters,
2020
IAssignmentDetails,

0 commit comments

Comments
 (0)