Skip to content

⬅️ Back to Table of Contents

📄 ExpiringCache

📊 Analysis Summary

Metric Count
🔧 Functions 3
🧱 Classes 1
📦 Imports 1
📊 Variables & Constants 2
📐 Interfaces 1

📚 Table of Contents

🛠️ File Location:

📂 packages/typescript-estree/src/parseSettings/ExpiringCache.ts

📦 Imports

Name Source
CacheDurationSeconds @typescript-eslint/types

Variables & Constants

Name Type Kind Value Exported
DEFAULT_TSCONFIG_CACHE_DURA... 30 const 30
ZERO_HR_TIME [number, number] const [0, 0]

Functions

ExpiringCache.clear(): void

Returns: void

Calls:

  • this.#map.clear
Code
clear(): void {
    this.#map.clear();
  }

ExpiringCache.get(key: Key): Value | undefined

Parameters:

  • key Key

Returns: Value | undefined

Calls:

  • this.#map.get
  • process.hrtime
  • this.#map.delete

Internal Comments:

// cache hit woo!
// key has expired - clean it up to free up memory (x5)
// no hit :'(

Code
get(key: Key): Value | undefined {
    const entry = this.#map.get(key);
    if (entry?.value != null) {
      if (this.#cacheDurationSeconds === 'Infinity') {
        return entry.value;
      }

      const ageSeconds = process.hrtime(entry.lastSeen)[0];
      if (ageSeconds < this.#cacheDurationSeconds) {
        // cache hit woo!
        return entry.value;
      }
      // key has expired - clean it up to free up memory
      this.#map.delete(key);
    }
    // no hit :'(
    return undefined;
  }

ExpiringCache.set(key: Key, value: Value): this

Parameters:

  • key Key
  • value Value

Returns: this

Calls:

  • this.#map.set
  • process.hrtime
Code
set(key: Key, value: Value): this {
    this.#map.set(key, {
      lastSeen:
        this.#cacheDurationSeconds === 'Infinity'
          ? // no need to waste time calculating the hrtime in infinity mode as there's no expiry
            ZERO_HR_TIME
          : process.hrtime(),
      value,
    });
    return this;
  }

Classes

ExpiringCache

A map with key-level expiration.

Implements: CacheLike<Key, Value>

Class Code
export class ExpiringCache<Key, Value> implements CacheLike<Key, Value> {
  readonly #cacheDurationSeconds: CacheDurationSeconds;

  readonly #map = new Map<
    Key,
    Readonly<{
      lastSeen: [number, number];
      value: Value;
    }>
  >();

  constructor(cacheDurationSeconds: CacheDurationSeconds) {
    this.#cacheDurationSeconds = cacheDurationSeconds;
  }

  clear(): void {
    this.#map.clear();
  }

  get(key: Key): Value | undefined {
    const entry = this.#map.get(key);
    if (entry?.value != null) {
      if (this.#cacheDurationSeconds === 'Infinity') {
        return entry.value;
      }

      const ageSeconds = process.hrtime(entry.lastSeen)[0];
      if (ageSeconds < this.#cacheDurationSeconds) {
        // cache hit woo!
        return entry.value;
      }
      // key has expired - clean it up to free up memory
      this.#map.delete(key);
    }
    // no hit :'(
    return undefined;
  }

  set(key: Key, value: Value): this {
    this.#map.set(key, {
      lastSeen:
        this.#cacheDurationSeconds === 'Infinity'
          ? // no need to waste time calculating the hrtime in infinity mode as there's no expiry
            ZERO_HR_TIME
          : process.hrtime(),
      value,
    });
    return this;
  }
}

Methods (3) — full entries under Functions

Method Signature
clear (): void
get (key: Key): Value \| undefined
set (key: Key, value: Value): this

Interfaces

CacheLike<Key, Value>

Interface Code
export interface CacheLike<Key, Value> {
  get(key: Key): Value | undefined;
  set(key: Key, value: Value): this;
}

Generated by Syntax Scribe