Skip to content

⬅️ Back to Table of Contents

📄 ExpiringCache.ts

📊 Analysis Summary

Metric Count
🔧 Functions 3
🧱 Classes 1
📦 Imports 1
📊 Variables & Constants 3
📐 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_DURATION_SECONDS 30 const 30
ZERO_HR_TIME [number, number] const [0, 0]
ageSeconds number const process.hrtime(entry.lastSeen)[0]

Functions

ExpiringCache.clear(): void

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

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

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;
  }
  • Parameters:
  • key: Key
  • Return Type: 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 :'(
    

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

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;
  }
  • Parameters:
  • key: Key
  • value: Value
  • Return Type: this
  • Calls:
  • this.#map.set
  • process.hrtime

Classes

ExpiringCache

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

clear(): void
Code
clear(): void {
    this.#map.clear();
  }
get(key: Key): Value | undefined
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;
  }
set(key: Key, value: Value): this
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;
  }

Interfaces

CacheLike<Key, Value>

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