Skip to content

Commit 001ae6e

Browse files
renaming classes and file organizing
1 parent e4d1d26 commit 001ae6e

File tree

8 files changed

+77
-59
lines changed

8 files changed

+77
-59
lines changed

src/cache/expiring-lru-in-memory-assignment-cache.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/cache/lru-cache.spec.ts

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ExpiringLRUCache, LRUCache } from './lru-cache';
1+
import { LRUCache } from './lru-cache';
22

33
describe('LRUCache', () => {
44
let cache: LRUCache;
@@ -62,28 +62,3 @@ describe('LRUCache', () => {
6262
expect(oneCache.get('b')).toBe('banana');
6363
});
6464
});
65-
66-
describe('Expiring LRU Cache', () => {
67-
let cache: ExpiringLRUCache;
68-
const expectedCacheTimeoutMs = 50;
69-
70-
beforeEach(async () => {
71-
cache = new ExpiringLRUCache(2, expectedCacheTimeoutMs);
72-
});
73-
74-
afterAll(async () => {
75-
jest.restoreAllMocks();
76-
});
77-
78-
it('should evict cache after timeout', async () => {
79-
jest.useFakeTimers();
80-
jest.spyOn(global, 'setTimeout');
81-
82-
cache.set('a', 'apple');
83-
jest.advanceTimersByTime(expectedCacheTimeoutMs);
84-
85-
expect(setTimeout).toHaveBeenCalledTimes(1);
86-
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), expectedCacheTimeoutMs);
87-
expect(cache.get('a')).toBeUndefined();
88-
});
89-
});

src/cache/lru-cache.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,20 +99,20 @@ export class LRUCache implements Map<string, string> {
9999
}
100100

101101
/**
102-
* Variation of LRUCache that expires after set time in milliseconds
102+
* Time-aware, least-recently-used (TLRU), variant of LRU where entries have valid lifetime.
103103
* @param {number} maxSize - Maximum cache size
104-
* @param {number} timeout - Time in milliseconds after which cache entry will evict itself
104+
* @param {number} ttl - Time in milliseconds after which cache entry will evict itself
105105
**/
106-
export class ExpiringLRUCache extends LRUCache {
107-
constructor(readonly maxSize: number, readonly timeout: number) {
106+
export class TLRUCache extends LRUCache {
107+
constructor(readonly maxSize: number, readonly ttl: number) {
108108
super(maxSize);
109109
}
110110

111111
set(key: string, value: string): this {
112112
const cache = super.set(key, value);
113113
setTimeout(() => {
114114
this.delete(key);
115-
}, this.timeout);
115+
}, this.ttl);
116116
return cache;
117117
}
118118
}

src/cache/tlru-cache.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { TLRUCache } from './lru-cache';
2+
3+
describe('TLRU Cache', () => {
4+
let cache: TLRUCache;
5+
const expectedCacheTimeoutMs = 50;
6+
7+
beforeEach(async () => {
8+
cache = new TLRUCache(2, expectedCacheTimeoutMs);
9+
});
10+
11+
afterAll(async () => {
12+
jest.restoreAllMocks();
13+
});
14+
15+
it('should evict cache after timeout', async () => {
16+
jest.useFakeTimers();
17+
jest.spyOn(global, 'setTimeout');
18+
19+
cache.set('a', 'apple');
20+
jest.advanceTimersByTime(expectedCacheTimeoutMs);
21+
22+
expect(setTimeout).toHaveBeenCalledTimes(1);
23+
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), expectedCacheTimeoutMs);
24+
expect(cache.get('a')).toBeUndefined();
25+
});
26+
});

src/cache/tlru-cache.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { LRUCache } from './lru-cache';
2+
3+
/**
4+
* Time-aware, least-recently-used (TLRU), variant of LRU where entries have valid lifetime.
5+
* @param {number} maxSize - Maximum cache size
6+
* @param {number} ttl - Time in milliseconds after which cache entry will evict itself
7+
**/
8+
export class TLRUCache extends LRUCache {
9+
constructor(readonly maxSize: number, readonly ttl: number) {
10+
super(maxSize);
11+
}
12+
13+
set(key: string, value: string): this {
14+
const cache = super.set(key, value);
15+
setTimeout(() => {
16+
this.delete(key);
17+
}, this.ttl);
18+
return cache;
19+
}
20+
}

src/cache/expiring-lru-in-memory-assignment-cache.spec.ts renamed to src/cache/tlru-in-memory-assignment-cache.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { ExpiringLRUInMemoryAssignmentCache } from './expiring-lru-in-memory-assignment-cache';
1+
import { TLRUInMemoryAssignmentCache } from './tlru-in-memory-assignment-cache';
22

33
describe('ExpiringLRUInMemoryAssignmentCache', () => {
4-
let cache: ExpiringLRUInMemoryAssignmentCache;
4+
let cache: TLRUInMemoryAssignmentCache;
55
const defaultTimout = 60_000; // 10 minutes
66

77
beforeAll(() => {
88
jest.useFakeTimers();
9-
cache = new ExpiringLRUInMemoryAssignmentCache(2);
9+
cache = new TLRUInMemoryAssignmentCache(2);
1010
});
1111

1212
it(`assignment cache's timeout should default to 10 minutes `, () => {
@@ -18,7 +18,7 @@ describe('ExpiringLRUInMemoryAssignmentCache', () => {
1818

1919
it(`assignment cache's timeout value is used on construction`, () => {
2020
const expectedTimout = 88;
21-
cache = new ExpiringLRUInMemoryAssignmentCache(2, expectedTimout);
21+
cache = new TLRUInMemoryAssignmentCache(2, expectedTimout);
2222
const key1 = { subjectKey: 'a', flagKey: 'b', banditKey: 'c', actionKey: 'd' };
2323
cache.set(key1);
2424
jest.advanceTimersByTime(expectedTimout);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { AbstractAssignmentCache } from './abstract-assignment-cache';
2+
import { TLRUCache } from './tlru-cache';
3+
4+
/**
5+
* Variation of LRU caching mechanism that will automatically evict items after
6+
* set time of milliseconds.
7+
*
8+
* It is used to limit the size of the cache.
9+
*
10+
* @param {number} maxSize - Maximum cache size
11+
* @param {number} ttl - Time in milliseconds after cache will expire.
12+
*/
13+
export class TLRUInMemoryAssignmentCache extends AbstractAssignmentCache<TLRUCache> {
14+
constructor(maxSize: number, ttl = 60_000) {
15+
super(new TLRUCache(maxSize, ttl));
16+
}
17+
}

src/client/eppo-client.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { IAssignmentEvent, IAssignmentLogger } from '../assignment-logger';
44
import { BanditEvaluator } from '../bandit-evaluator';
55
import { IBanditEvent, IBanditLogger } from '../bandit-logger';
66
import { AssignmentCache } from '../cache/abstract-assignment-cache';
7-
import { ExpiringLRUInMemoryAssignmentCache } from '../cache/expiring-lru-in-memory-assignment-cache';
87
import { LRUInMemoryAssignmentCache } from '../cache/lru-in-memory-assignment-cache';
98
import { NonExpiringInMemoryAssignmentCache } from '../cache/non-expiring-in-memory-cache-assignment';
9+
import { TLRUInMemoryAssignmentCache } from '../cache/tlru-in-memory-assignment-cache';
1010
import ConfigurationRequestor from '../configuration-requestor';
1111
import { IConfigurationStore } from '../configuration-store/configuration-store';
1212
import {
@@ -983,15 +983,15 @@ export default class EppoClient {
983983
}
984984

985985
public useLRUInMemoryBanditAssignmentCache(maxSize: number) {
986-
this.banditAssignmentCache = new LRUInMemoryAssignmentCache(maxSize);
986+
this.banditAssignmentCache = new TLRUInMemoryAssignmentCache(maxSize);
987987
}
988988

989989
/**
990990
* @param {number} maxSize - Maximum cache size
991991
* @param {number} timeout - TTL of cache entries
992992
*/
993-
public useExpiringInMemoryBanditAssignmentCache(maxSize: number, timeout?: number) {
994-
this.banditAssignmentCache = new ExpiringLRUInMemoryAssignmentCache(maxSize, timeout);
993+
public useTLRUInMemoryAssignmentCache(maxSize: number, timeout?: number) {
994+
this.banditAssignmentCache = new TLRUInMemoryAssignmentCache(maxSize, timeout);
995995
}
996996

997997
public useCustomBanditAssignmentCache(cache: AssignmentCache) {

0 commit comments

Comments
 (0)