* Clears the cache. * * @experimental **UNSTABLE**: New API, yet to be vetted. * * @example Usage * ```ts * import { LruCache } from "@std/cache"; * import { assertEquals } from "@std/assert/equals"; * * const cache = new LruCache (100); * * cache.set(
()
| 333 | * ``` |
| 334 | */ |
| 335 | override clear(): void { |
| 336 | if (this.#ejecting) { |
| 337 | throw new TypeError( |
| 338 | "Cannot clear LruCache: cache is not re-entrant during onEject callbacks", |
| 339 | ); |
| 340 | } |
| 341 | if (!this.#eject) { |
| 342 | super.clear(); |
| 343 | return; |
| 344 | } |
| 345 | const entries = [...super.entries()]; |
| 346 | super.clear(); |
| 347 | this.#ejecting = true; |
| 348 | let error: unknown; |
| 349 | try { |
| 350 | for (const [key, value] of entries) { |
| 351 | try { |
| 352 | this.#eject(key, value, "cleared"); |
| 353 | } catch (e) { |
| 354 | error ??= e; |
| 355 | } |
| 356 | } |
| 357 | } finally { |
| 358 | this.#ejecting = false; |
| 359 | } |
| 360 | if (error !== undefined) throw error; |
| 361 | } |
| 362 | } |