Skip to content

Commit b68bf7f

Browse files
committed
address review feedback
1 parent ff25ce0 commit b68bf7f

2 files changed

Lines changed: 13 additions & 27 deletions

File tree

packages/connectivity/src/scp-cf/cache.spec.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,19 +201,14 @@ describe('Cache', () => {
201201
});
202202

203203
describe('getOrInsertComputed', () => {
204-
it('calls computeFn on cache miss and stores result', () => {
205-
const compute = jest.fn().mockReturnValue({ entry: destinationOne });
206-
const result = cacheOne.getOrInsertComputed('new', compute);
207-
expect(result).toEqual(destinationOne);
208-
expect(compute).toHaveBeenCalledTimes(1);
209-
});
210-
211204
it('returns cached value on subsequent calls without calling computeFn again', () => {
212205
const compute = jest.fn().mockReturnValue({ entry: destinationOne });
213206
cacheOne.getOrInsertComputed('cached', compute);
214207
const result = cacheOne.getOrInsertComputed('cached', compute);
215208
expect(result).toEqual(destinationOne);
216209
expect(compute).toHaveBeenCalledTimes(1);
210+
const result2 = cacheOne.get('cached');
211+
expect(result2).toEqual(destinationOne);
217212
});
218213

219214
it('stores a custom expiration returned by computeFn', () => {

packages/connectivity/src/scp-cf/cache.ts

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ interface CacheInterface<T> {
66
get(key: string | undefined): T | undefined;
77
set(key: string | undefined, item: CacheEntry<T>): void;
88
clear(): void;
9-
getOrInsertComputed(
10-
key: string | undefined,
11-
computeFn: () => CacheEntry<T>
12-
): T;
9+
getOrInsertComputed(key: string, computeFn: () => CacheEntry<T>): T;
1310
}
1411

1512
/**
@@ -51,11 +48,11 @@ export class Cache<T> implements CacheInterface<T> {
5148
/**
5249
* Creates an instance of Cache.
5350
* @param defaultValidityTime - The default validity time in milliseconds. Use 0 for unlimited cache duration.
54-
* @param maxSize - The maximum number of entries in the cache. Use Infinity for unlimited size. Items are evicted based on a least recently used (LRU) strategy.
51+
* @param capacity - The maximum number of entries in the cache. Use Infinity for unlimited size. Items are evicted based on a least recently used (LRU) strategy.
5552
*/
5653
constructor(
5754
private defaultValidityTime: number,
58-
private maxSize = Infinity
55+
private capacity = Infinity
5956
) {
6057
this.cache = new Map<string, CacheEntry<T>>();
6158
}
@@ -91,14 +88,14 @@ export class Cache<T> implements CacheInterface<T> {
9188
}
9289

9390
if (isExpired(entry)) {
94-
this.cache.delete(key!);
91+
this.cache.delete(key);
9592
return undefined;
9693
}
9794

9895
// LRU cache: Move accessed entry to the end of the Map to mark it as recently used
99-
if (this.maxSize !== Infinity) {
100-
this.cache.delete(key!);
101-
this.cache.set(key!, entry);
96+
if (this.capacity !== Infinity) {
97+
this.cache.delete(key);
98+
this.cache.set(key, entry);
10299
}
103100
return entry?.entry;
104101
}
@@ -113,15 +110,15 @@ export class Cache<T> implements CacheInterface<T> {
113110
return;
114111
}
115112
if (
116-
this.cache.size >= this.maxSize &&
117-
this.cache.size > 0 &&
113+
this.cache.size >= this.capacity &&
114+
this.cache.size &&
118115
!this.cache.has(key)
119116
) {
120117
// Evict the least recently used (LRU) entry
121118
const lruKey = this.cache.keys().next().value;
122119
this.cache.delete(lruKey!); // SAFETY: size > 0
123120
}
124-
if (this.maxSize !== Infinity && this.cache.has(key)) {
121+
if (this.capacity !== Infinity && this.cache.has(key)) {
125122
// If the key already exists, delete it to update its position in the LRU order
126123
this.cache.delete(key);
127124
}
@@ -130,13 +127,7 @@ export class Cache<T> implements CacheInterface<T> {
130127
this.cache.set(key, { entry: item.entry, expires });
131128
}
132129

133-
getOrInsertComputed(
134-
key: string | undefined,
135-
computeFn: () => CacheEntry<T>
136-
): T {
137-
if (!key) {
138-
return computeFn().entry;
139-
}
130+
getOrInsertComputed(key: string, computeFn: () => CacheEntry<T>): T {
140131
const cachedEntry = this.get(key);
141132
if (cachedEntry !== undefined) {
142133
return cachedEntry;

0 commit comments

Comments
 (0)