Skip to content

Commit 6695a2b

Browse files
committed
review fix
1 parent f1c4c83 commit 6695a2b

File tree

2 files changed

+37
-42
lines changed

2 files changed

+37
-42
lines changed

packages/open-next/src/overrides/incrementalCache/multi-tier-ddb-s3.ts

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,22 @@
11
import type { CacheValue, IncrementalCache } from "types/overrides";
22
import { customFetchClient } from "utils/fetch";
3+
import { LRUCache } from "utils/lru";
34
import { debug } from "../../adapters/logger";
45
import S3Cache, { getAwsClient } from "./s3-lite";
56

67
// TTL for the local cache in milliseconds
7-
const localCacheTTL = process.env.OPEN_NEXT_LOCAL_CACHE_TTL
8-
? Number.parseInt(process.env.OPEN_NEXT_LOCAL_CACHE_TTL)
8+
const localCacheTTL = process.env.OPEN_NEXT_LOCAL_CACHE_TTL_MS
9+
? Number.parseInt(process.env.OPEN_NEXT_LOCAL_CACHE_TTL_MS, 10)
910
: 0;
1011
// Maximum size of the local cache in nb of entries
1112
const maxCacheSize = process.env.OPEN_NEXT_LOCAL_CACHE_SIZE
12-
? Number.parseInt(process.env.OPEN_NEXT_LOCAL_CACHE_SIZE)
13+
? Number.parseInt(process.env.OPEN_NEXT_LOCAL_CACHE_SIZE, 10)
1314
: 1000;
1415

15-
class LRUCache {
16-
private cache: Map<
17-
string,
18-
{
19-
value: CacheValue<boolean>;
20-
lastModified: number;
21-
}
22-
> = new Map();
23-
private maxSize: number;
24-
25-
constructor(maxSize: number) {
26-
this.maxSize = maxSize;
27-
}
28-
29-
// isFetch is not used here, only used for typing
30-
get<T extends boolean = false>(key: string, isFetch?: T) {
31-
return this.cache.get(key) as {
32-
value: CacheValue<T>;
33-
lastModified: number;
34-
};
35-
}
36-
37-
set(key: string, value: any) {
38-
if (this.cache.size >= this.maxSize) {
39-
const firstKey = this.cache.keys().next().value;
40-
if (firstKey) {
41-
this.cache.delete(firstKey);
42-
}
43-
}
44-
this.cache.set(key, value);
45-
}
46-
47-
delete(key: string) {
48-
this.cache.delete(key);
49-
}
50-
}
51-
52-
const localCache = new LRUCache(maxCacheSize);
16+
const localCache = new LRUCache<{
17+
value: CacheValue<false>;
18+
lastModified: number;
19+
}>(maxCacheSize);
5320

5421
const awsFetch = (body: RequestInit["body"], type: "get" | "set" = "get") => {
5522
const { CACHE_BUCKET_REGION } = process.env;
@@ -85,7 +52,7 @@ const multiTierCache: IncrementalCache = {
8552
name: "multi-tier-ddb-s3",
8653
async get(key, isFetch) {
8754
// First we check the local cache
88-
const localCacheEntry = localCache.get(key, isFetch);
55+
const localCacheEntry = localCache.get(key);
8956
if (localCacheEntry) {
9057
if (Date.now() - localCacheEntry.lastModified < localCacheTTL) {
9158
debug("Using local cache without checking ddb");
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
export class LRUCache<T> {
2+
private cache: Map<string, T> = new Map();
3+
4+
constructor(private maxSize: number) {}
5+
6+
get(key: string) {
7+
const result = this.cache.get(key);
8+
if (result) {
9+
this.cache.delete(key);
10+
this.cache.set(key, result);
11+
}
12+
return result;
13+
}
14+
15+
set(key: string, value: any) {
16+
if (this.cache.size >= this.maxSize) {
17+
const firstKey = this.cache.keys().next().value;
18+
if (firstKey) {
19+
this.cache.delete(firstKey);
20+
}
21+
}
22+
this.cache.set(key, value);
23+
}
24+
25+
delete(key: string) {
26+
this.cache.delete(key);
27+
}
28+
}

0 commit comments

Comments
 (0)