| ... | ... | @@ -1,4 +1,5 @@ |
| 1 | 1 | /** |
| 2 | * Least-recently-used cache. |
| 2 | 3 | * This module is intended to be imported via the main class. |
| 3 | 4 | * |
| 4 | 5 | * import { Lru } from '@clo/lib/Lru'; |
| ... | ... | @@ -12,19 +13,19 @@ |
| 12 | 13 | interface Options<K, V> { |
| 13 | 14 | capacity: number; |
| 14 | 15 | /** |
| 15 | | * Decide how many units of size each entry takes. |
| 16 | * decide how many units of size each entry takes. |
| 16 | 17 | * For example, accessing `.byteLength` of a Buffer. |
| 17 | 18 | */ |
| 18 | 19 | sizeFn?: (value: V, key: K) => number; |
| 19 | | /** Called when `set` or `ensureUnusedCapacity` removes items. */ |
| 20 | /** called when `set` or `ensureUnusedCapacity` removes items. */ |
| 20 | 21 | evict?: (value: V, key: K) => void; |
| 21 | | /** Called when anything removes items. */ |
| 22 | /** called when anything removes items. */ |
| 22 | 23 | delete?: (value: V, key: K) => void; |
| 23 | 24 | } |
| 24 | 25 | |
| 25 | 26 | /** |
| 26 | | * A Least-recently-used cache has a set capacity, removing the oldest item |
| 27 | | * when trying to insert an item that would exceed such capacity. This |
| 27 | * a Least-recently-used cache has a set capacity, removing the oldest item |
| 28 | * when trying to insert an item that would exceed such capacity. this |
| 28 | 29 | * implementation supports variable sized items, eviction callbacks, entry |
| 29 | 30 | * locking, and JSON-compatible serialization. |
| 30 | 31 | * |
| ... | ... | @@ -32,9 +33,9 @@ interface Options<K, V> { |
| 32 | 33 | */ |
| 33 | 34 | export class Lru<K, V> extends Map<K, V> { |
| 34 | 35 | #order = new Map<K, Order<K>>(); |
| 35 | | /** The most recently used */ |
| 36 | /** the most recently used */ |
| 36 | 37 | #head: K | none = none; |
| 37 | | /** The least recently used */ |
| 38 | /** the least recently used */ |
| 38 | 39 | #tail: K | none = none; |
| 39 | 40 | #used: number = 0; |
| 40 | 41 | #locked: number = 0; |
| ... | ... | @@ -119,7 +120,7 @@ export class Lru<K, V> extends Map<K, V> { |
| 119 | 120 | return this; |
| 120 | 121 | } |
| 121 | 122 | |
| 122 | | /** Prevent an item from being automatically evicted */ |
| 123 | /** prevent an item from being automatically evicted */ |
| 123 | 124 | lock(key: K): ts.Dispose { |
| 124 | 125 | const order = this.#order.get(key); |
| 125 | 126 | if (!order) throw new Error(`Key ${key} not in Lru`); |
| ... | ... | @@ -131,7 +132,7 @@ export class Lru<K, V> extends Map<K, V> { |
| 131 | 132 | }); |
| 132 | 133 | } |
| 133 | 134 | |
| 134 | | /** Evict items until there are `unused` capacity slots. */ |
| 135 | /** evict items until there are `unused` capacity slots. */ |
| 135 | 136 | ensureUnusedCapacity(unused: number) { |
| 136 | 137 | const uc = this.unlockedCapacity; |
| 137 | 138 | if (unused > uc) { |
| ... | ... | @@ -171,7 +172,7 @@ export class Lru<K, V> extends Map<K, V> { |
| 171 | 172 | this.#used -= needed - remain; |
| 172 | 173 | } |
| 173 | 174 | |
| 174 | | /** Unlink an entry without removing it. */ |
| 175 | /** unlink an entry without removing it. */ |
| 175 | 176 | #evict(key: K) { |
| 176 | 177 | const entry = this.#order.get(key); |
| 177 | 178 | if (!entry) return null; |
| ... | ... | @@ -243,7 +244,7 @@ export class Lru<K, V> extends Map<K, V> { |
| 243 | 244 | } |
| 244 | 245 | } |
| 245 | 246 | |
| 246 | | /** The first item is the most recently used. */ |
| 247 | /** the first item is the most recently used. */ |
| 247 | 248 | type Serialized<K, V> = Array<[K, V, number]>; |
| 248 | 249 | |
| 249 | 250 | interface SerializeOptions<IK, IV, OK = IK, OV = IV> { |
| ... | ... | @@ -252,13 +253,13 @@ interface SerializeOptions<IK, IV, OK = IK, OV = IV> { |
| 252 | 253 | } |
| 253 | 254 | |
| 254 | 255 | type Order<K> = [ |
| 255 | | /** Number of units computed at insertion time. */ |
| 256 | /** number of units computed at insertion time. */ |
| 256 | 257 | size: number, |
| 257 | | /** Towards most recently used */ |
| 258 | /** towards most recently used */ |
| 258 | 259 | before: K | none, |
| 259 | | /** Towards least recently used */ |
| 260 | /** towards least recently used */ |
| 260 | 261 | after: K | none, |
| 261 | | /** Locking prevents automatic eviction */ |
| 262 | /** locking prevents automatic eviction */ |
| 262 | 263 | locks: number, |
| 263 | 264 | ]; |
| 264 | 265 | |