Skip to content

Latest commit

 

History

History
30 lines (24 loc) · 1.06 KB

README.md

File metadata and controls

30 lines (24 loc) · 1.06 KB

LRU Cache

Typescript LRU Cache implementation https://en.wikipedia.org/wiki/Cache_replacement_policies#Least_recently_used_(LRU)

Implementation

Using hash and doubly linked list allows to perform

  • set operation in O(1) complexity
  • get operation in O(1) complexity

Usage

import {LRUCache} from "lru-cache";

let cache = new LRUCache<number, string>(3) // initialize the cache with size 3
cache.set(1, "one");
cache.set(2, "two");
cache.set(3, "three");
cache.isFull(); // true
cache.get(3) // "three"
// now the cache is holding 1, 2 and 3 keys
cache.set(4, "four") // will delete (1, "one") as the least recently used key
cache.get(1) // null
cache.get(2) // will set key 2 as most recently used
cache.set(5, "five") // will delete (3, "three")
cache.get(2) // "two"