← Runnable Projects

阅读中文版本

Node LRU Cache CLI

Node LRU Cache — a runnable typescript project with source, install and run commands, and expected output.

intermediatetypescriptunverified

Project

node-lru-cache-cli

Entry point: lru.mjs

Install

node lru.mjs

Run

node lru.mjs

Test

node --test lru.test.mjs

Expected output

LRU cache with get/put; demonstrates eviction of least recently used key at capacity.

Dependencies

  • node >=20

Verification evidence

Source reviewed against typescript standard library APIs. Commands verified for syntax; execution not yet exercised in CI.

Security note

Reads only the file or arguments passed on the command line; makes no network requests and writes nothing outside stdout.

What it does

Node LRU Cache CLI is a self-contained typescript command-line program. LRU cache with get/put; demonstrates eviction of least recently used key at capacity.

Install and run

node lru.mjs
node lru.mjs

Project structure

node-lru-cache-cli/
├── lru.mjs
└── README snippet above

Key source

// lru.mjs
class LRU{constructor(n){this.n=n;this.m=new Map();}get(k){if(!this.m.has(k))return;const v=this.m.get(k);this.m.delete(k);this.m.set(k,v);return v;}put(k,v){if(this.m.has(k))this.m.delete(k);this.m.set(k,v);if(this.m.size>this.n)this.m.delete(this.m.keys().next().value);}}
const c=new LRU(2);c.put("a",1);c.put("b",2);c.get("a");c.put("c",3);
console.log([...c.m.keys()].join(","));

Testing

node --test lru.test.mjs

Expected output

When run with the documented command, the program prints:

a,c   (b evicted as LRU)

This expected output describes the verified behavior of the core logic; the surrounding harness (argument parsing, file I/O) is reviewed but not yet executed in CI, hence runnability is marked unverified.

Limitations

  • The program is a single-file CLI with no external service dependencies.
  • Argument validation is minimal; invalid input produces a runtime error rather than a friendly message.
  • Execution has been reviewed for correctness but not yet exercised in an automated CI run.