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
15 changes: 15 additions & 0 deletions src/cache/lru-cache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,19 @@ describe('LRUCache', () => {
expect(oneCache.get('a')).toBeFalsy();
expect(oneCache.get('b')).toBe('banana');
});

/**
This test case might be an overkill but in case Map() changes,
Copy link
Contributor

Choose a reason for hiding this comment

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

probably handled by a test like your "should evict oldest entry when capacity limit is reached" but no harm no foul

or we want to ditch it completely this will remind us that insertion
order is crucial for this cache to work properly
**/
it('should preserve insertion order when inserting on capacity limit', () => {
cache.set('a', 'apple');
cache.set('b', 'banana');
cache.set('c', 'cherry');

const keys = Array.from(cache.keys());
expect(keys[0]).toBe('b');
expect(keys[1]).toBe('c');
});
});
82 changes: 75 additions & 7 deletions src/cache/tlru-cache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,65 @@ describe('TLRU Cache', () => {
expect(cache.get('a')).toBeUndefined();
});

it('should evict all expired entries', () => {
it('should not evict cache before expiration', () => {
jest.useFakeTimers();

cache.set('a', 'apple');
jest.advanceTimersByTime(expectedCacheTimeoutMs - 1);
expect(cache.get('a')).toBe('apple');
});

it('should evict all expired entries on .entries() call', () => {
jest.useFakeTimers();

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

expect(cache.get('b')).toBeUndefined();
expect(cache.get('a')).toBeUndefined();
const cacheEntries = [];

for (const entry of cache.entries()) {
cacheEntries.push(entry);
}

expect(cacheEntries.length).toBe(0);
});

it('should evict all expired entries on .keys() call', () => {
jest.useFakeTimers();

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

const cacheKeys = [];

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

expect(cacheKeys.length).toBe(0);
Copy link
Contributor

Choose a reason for hiding this comment

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

probably good to leave one non-expired key in here to make sure it's still there

});

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

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

const cacheValues = [];

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

expect(cacheValues.length).toBe(0);
Copy link
Contributor

Choose a reason for hiding this comment

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

same here

});

/**
* This test assumes implementation which is not ideal, but that's
* the only way I know of how to go around timers in jest
**/
it('should overwrite existing cache entry', () => {
jest.useFakeTimers();

Expand Down Expand Up @@ -88,4 +131,29 @@ describe('TLRU Cache', () => {
expect(oneCache.get('a')).toBeFalsy();
expect(oneCache.get('b')).toBe('banana');
});

it('should evict oldest entry when capacity limit is reached', () => {
cache.set('a', 'apple');
cache.set('b', 'banana');
cache.set('c', 'cherry');

expect(cache.get('a')).toBeUndefined();
expect(cache.has('b')).toBeTruthy();
expect(cache.has('c')).toBeTruthy();
});

/**
This test case might be an overkill but in case Map() changes,
or we want to ditch it completely this will remind us that insertion
order is crucial for this cache to work properly
**/
it('should preserve insertion order when inserting on capacity limit', () => {
cache.set('a', 'apple');
cache.set('b', 'banana');
cache.set('c', 'cherry');

const keys = Array.from(cache.keys());
expect(keys[0]).toBe('b');
expect(keys[1]).toBe('c');
});
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)

});
55 changes: 46 additions & 9 deletions src/cache/tlru-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ export class TLRUCache extends LRUCache {
}
}

private isCacheEntryValid(key: string): boolean {
const now = new Date(Date.now());
const evictionDate = this.cacheEntriesTTLRegistry.get(key);
return evictionDate !== undefined ? now < evictionDate : false;
}

private setCacheEntryEvictionTime(key: string): void {
this.cacheEntriesTTLRegistry.set(key, this.getCacheEntryEvictionTime());
}
Expand All @@ -32,32 +38,65 @@ export class TLRUCache extends LRUCache {
}

private evictExpiredCacheEntries() {
const now = new Date(Date.now());
let cacheKey: string;
let evictionDate: Date;

// Not using this.cache.forEach so we can break the loop once
// we find the fist non-expired entry. Each entry after that
// is guaranteed to also be non-expired, because they are oldest->newest
for ([cacheKey, evictionDate] of this.cacheEntriesTTLRegistry.entries()) {
if (now >= evictionDate) {
// is guaranteed to also be non-expired, because iteration happens
// in insertion order
for (cacheKey of this.cache.keys()) {
if (!this.isCacheEntryValid(cacheKey)) {
this.delete(cacheKey);
} else {
break;
}
}
}

entries(): IterableIterator<[string, string]> {
this.evictExpiredCacheEntries();
return super.entries();
}

keys(): IterableIterator<string> {
this.evictExpiredCacheEntries();
return super.keys();
}

values(): IterableIterator<string> {
this.evictExpiredCacheEntries();
return super.values();
}

delete(key: string): boolean {
this.clearCacheEntryEvictionTimeIfExists(key);
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;
// }

get(key: string): string | undefined {
this.evictExpiredCacheEntries();
if (!this.cache.has(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;
}

Copy link
Contributor

Choose a reason for hiding this comment

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

otherwise, if valid, do we need to this.cache.set(key, value); to bump it to the bottom for the LRU like we do the parent class?

I wonder if we just check !this.isCacheEntryValid(key) first, if so delete it from the cache, then proceed to just call the parent method's get()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great catch Aaron 🙏 Such a stupid mistake, my spirit with this cache implementation was crushed at the moment of writing this method 🫠 Your concerns saved my ass here!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wonder if we just check !this.isCacheEntryValid(key) first, if so delete it from the cache, then proceed to just call the parent method's get()

I've rewrote it to do just that + reset cache TTL timer if we got a cache hit from parent class

// 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 All @@ -68,8 +107,6 @@ export class TLRUCache extends LRUCache {
}

set(key: string, value: string): this {
this.evictExpiredCacheEntries();

const cache = super.set(key, value);
this.resetCacheEntryEvictionTime(key);
return cache;
Expand Down
Loading