import { clamp } from '../utils/common' ; internal export class LRUCache < K , V > { private readonly max : number ; private readonly cache : Map < K , V > ; constructor ( max = 10 ) { this . max = clamp ( max , 1 , Infinity ) ; this . cache = new Map ( ) ; } get ( key ) { const item = this . cache . get ( key ) ; if ( item ) { this . cache . delete ( key ) ; this . cache . set ( key , item ) ; } return item ; } set ( key , val ) { if ( this . cache . has ( key ) ) this . cache . delete ( key ) ; else if ( this . cache . size === this . max ) this . cache . delete ( this . first ( ) ) ; this . cache . set ( key , val ) ; } first ( ) { return this . cache . keys ( ) . next ( ) . value ; } }