|  | 
| 1 | 1 | import type { CacheValue, IncrementalCache } from "types/overrides"; | 
| 2 | 2 | import { customFetchClient } from "utils/fetch"; | 
|  | 3 | +import { LRUCache } from "utils/lru"; | 
| 3 | 4 | import { debug } from "../../adapters/logger"; | 
| 4 | 5 | import S3Cache, { getAwsClient } from "./s3-lite"; | 
| 5 | 6 | 
 | 
| 6 | 7 | // 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) | 
| 9 | 10 |   : 0; | 
| 10 | 11 | // Maximum size of the local cache in nb of entries | 
| 11 | 12 | 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) | 
| 13 | 14 |   : 1000; | 
| 14 | 15 | 
 | 
| 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); | 
| 53 | 20 | 
 | 
| 54 | 21 | const awsFetch = (body: RequestInit["body"], type: "get" | "set" = "get") => { | 
| 55 | 22 |   const { CACHE_BUCKET_REGION } = process.env; | 
| @@ -85,7 +52,7 @@ const multiTierCache: IncrementalCache = { | 
| 85 | 52 |   name: "multi-tier-ddb-s3", | 
| 86 | 53 |   async get(key, isFetch) { | 
| 87 | 54 |     // First we check the local cache | 
| 88 |  | -    const localCacheEntry = localCache.get(key, isFetch); | 
|  | 55 | +    const localCacheEntry = localCache.get(key); | 
| 89 | 56 |     if (localCacheEntry) { | 
| 90 | 57 |       if (Date.now() - localCacheEntry.lastModified < localCacheTTL) { | 
| 91 | 58 |         debug("Using local cache without checking ddb"); | 
|  | 
0 commit comments