Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/cache/tlru-cache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,35 +50,41 @@ describe('TLRU Cache', () => {
it('should evict all expired entries on .keys() call', () => {
jest.useFakeTimers();

cache = new TLRUCache(3, expectedCacheTimeoutMs);
cache.set('a', 'avocado');
jest.advanceTimersByTime(expectedCacheTimeoutMs);
cache.set('b', 'banana');
jest.advanceTimersByTime(expectedCacheTimeoutMs);
cache.set('c', 'cherry');

const cacheKeys = [];

for (const key of cache.keys()) {
cacheKeys.push(key);
}

expect(cacheKeys.length).toBe(0);
expect(cacheKeys.length).toBe(1);
expect(cache.get('c')).toBe('cherry');
Comment on lines +66 to +67
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

});

it('should evict all expired entries on .values() call', () => {
jest.useFakeTimers();
cache = new TLRUCache(3, expectedCacheTimeoutMs);

cache.set('a', 'avocado');
jest.advanceTimersByTime(expectedCacheTimeoutMs);
cache.set('b', 'banana');
jest.advanceTimersByTime(expectedCacheTimeoutMs);
cache.set('c', 'cherry');

const cacheValues = [];

for (const value of cache.values()) {
cacheValues.push(value);
}

expect(cacheValues.length).toBe(0);
expect(cacheValues.length).toBe(1);
expect(cache.get('c')).toBe('cherry');
Comment on lines +86 to +87
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

});

it('should overwrite existing cache entry', () => {
Expand Down Expand Up @@ -152,8 +158,18 @@ describe('TLRU Cache', () => {
cache.set('b', 'banana');
cache.set('c', 'cherry');

const keys = Array.from(cache.keys());
let keys = Array.from(cache.keys());
expect(keys[0]).toBe('b');
expect(keys[1]).toBe('c');

cache = new TLRUCache(2, expectedCacheTimeoutMs);
cache.set('a', 'apple');
cache.set('b', 'banana');
cache.get('a');
cache.set('c', 'cherry');

keys = Array.from(cache.keys());
expect(keys[0]).toBe('a');
expect(keys[1]).toBe('c');
Comment on lines +171 to +173
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💪

});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need another test that does:

cache.set('a', 'apple');
cache.set('b', 'banana');
cache.get('a');
cache.set('c', 'cherry');

Then makes sure its c and a (e.g., a got re-inserted at the end)

});
28 changes: 10 additions & 18 deletions src/cache/tlru-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,30 +73,22 @@ export class TLRUCache extends LRUCache {
return super.delete(key);
}

// has(key: string): boolean {
// const hasValue = this.cache.has(key);
//
// if (!this.isCacheEntryValid(key)) {
// this.delete(key);
// return false;
// }
//
// return hasValue;
// }
has(key: string): boolean {
if (!this.isCacheEntryValid(key)) {
this.delete(key);
return false;
}
return this.cache.has(key);
}

get(key: string): string | undefined {
if (!this.cache.has(key)) {
if (!this.isCacheEntryValid(key)) {
this.delete(key);
return undefined;
}

const value = this.cache.get(key);

const value = super.get(key);
if (value !== undefined) {
if (!this.isCacheEntryValid(key)) {
this.delete(key);
return undefined;
}

// Whenever we get a cache hit, we need to reset the timer
// for eviction, because it is now considered most recently
// accessed thus the timer should start over. Not doing that
Expand Down