deno.land / x / lru_map@1.0.0-beta.1 / lru_map.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2022-latest TomokiMiyauci. All rights reserved. MIT license.// This module is browser compatible.
// deno-lint-ignore-file no-explicit-any no-empty-interface
export interface LRUMapConstructor { new <K, V>( maxSize: number, entries?: readonly (readonly [K, V])[] | null, ): Map<K, V>;
readonly prototype: LRUMap<any, any>;}
function _createLRUMap<K, V>( maxSize: number, iterable: Iterable<readonly [K, V]> | null | undefined,): Map<K, V> { class LRUMap extends Map<K, V> { override set(key: K, value: V): this { if (this.has(key)) return this.rollup(key, value);
if (maxSize <= this.size) this.delete(this.firstKey);
if (maxSize > this.size) return super.set(key, value);
return this; }
override get(key: K): V | undefined { const has = this.has(key); const value = super.get(key);
if (has) { this.rollup(key, value!); }
return value; }
private rollup(key: K, value: V): this { super.delete(key); return super.set(key, value); }
private get firstKey(): K { return this.keys().next().value; } }
return new LRUMap(iterable);}
/** LRU cache based on `Map` object. * * ```ts * import { LRUMap } from "https://deno.land/x/lru_map@$VERSION/mod.ts"; * const cache = new LRUMap(100); * ``` */export const LRUMap: LRUMapConstructor = _createLRUMap as unknown as LRUMapConstructor;
export interface LRUMap<K, V> extends Map<K, V> {}
lru_map

Version Info

Tagged at
a year ago