-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
assignment logging cache prevents duplicate invocations (FF-1069) (#25)
* assignment logging cache prevents duplicate invocations (FF-1069) * add flagKey and remove variationKey from assignment cache key * additional test cases * additional test case. improve naming/clarity * rename hasAssigned to hasLoggedAssignment * add hashed variation value to cache key; handle case of variation changing * add comments to test * handle variation changing * fix comment * refactor into abstract class * instantiate correctly * remove debug * remove import * rename to setLastLoggedAssignment * split into 3 publish methods * fix comment * remove type * remove unused comment * refactor into beforeEach block * require node version 16 * bump to 1.7.0 --------- Co-authored-by: Eric Petzel <[email protected]>
- Loading branch information
1 parent
b6f2fe6
commit 71cd364
Showing
10 changed files
with
361 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
engine-strict=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { LRUCache } from 'lru-cache'; | ||
|
||
import { EppoValue } from './eppo_value'; | ||
|
||
export interface AssignmentCacheKey { | ||
subjectKey: string; | ||
flagKey: string; | ||
allocationKey: string; | ||
variationValue: EppoValue; | ||
} | ||
|
||
export interface Cacheable { | ||
get(key: string): string | undefined; | ||
set(key: string, value: string): void; | ||
has(key: string): boolean; | ||
} | ||
|
||
export abstract class AssignmentCache<T extends Cacheable> { | ||
// key -> variation value hash | ||
protected cache: T; | ||
|
||
constructor(cacheInstance: T) { | ||
this.cache = cacheInstance; | ||
} | ||
|
||
hasLoggedAssignment(key: AssignmentCacheKey): boolean { | ||
// no cache key present | ||
if (!this.cache.has(this.getCacheKey(key))) { | ||
return false; | ||
} | ||
|
||
// the subject has been assigned to a different variation | ||
// than was previously logged. | ||
// in this case we need to log the assignment again. | ||
if (this.cache.get(this.getCacheKey(key)) !== key.variationValue.toHashedString()) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
setLastLoggedAssignment(key: AssignmentCacheKey): void { | ||
this.cache.set(this.getCacheKey(key), key.variationValue.toHashedString()); | ||
} | ||
|
||
protected getCacheKey({ subjectKey, flagKey, allocationKey }: AssignmentCacheKey): string { | ||
return [`subject:${subjectKey}`, `flag:${flagKey}`, `allocation:${allocationKey}`].join(';'); | ||
} | ||
} | ||
|
||
/** | ||
* A cache that never expires. | ||
* | ||
* 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>> { | ||
constructor() { | ||
super(new Map<string, string>()); | ||
} | ||
} | ||
|
||
/** | ||
* A cache that uses the LRU algorithm to evict the least recently used items. | ||
* | ||
* It is used to limit the size of the cache. | ||
* | ||
* The primary use case is for server-side SDKs, where the cache is shared across | ||
* 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>> { | ||
constructor(maxSize: number) { | ||
super(new LRUCache<string, string>({ max: maxSize })); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.