# leastRecentlyUsedCache

> **leastRecentlyUsedCache**<`K`, `V`>(`options`): [`Cache`](/reference/knotkit/cache/interfaces/cache/)<`K`, `V`>

Defined in: [packages/cache/src/least-recently-used-cache.ts:57](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/cache/src/least-recently-used-cache.ts#L57)

Create a [Cache](/reference/knotkit/cache/interfaces/cache/) that evicts the entry used least recently.

Choose it when a key read recently is likely to be read again soon and the traffic has no long one-pass scans: a scan of keys never read again pushes every older entry out. `get` and a `set` of a key already held count as uses; `peek` and `has` do not.

`get`, `peek`, `has`, `delete`, and `set` run in O(1) average time, plus O(1) per entry `set` evicts. Iteration is O(n) over every slot allocated since the last `clear`.

## Type Parameters

### K

`K`

### V

`V`

## Parameters

### options

[`CacheOptions`](/reference/knotkit/cache/interfaces/cacheoptions/)<`K`, `V`>

## Returns

[`Cache`](/reference/knotkit/cache/interfaces/cache/)<`K`, `V`>

## Throws

With `INVALID_CAPACITY` when `capacity` is not a positive safe integer (or, with `weigh`, not a positive finite number), `MISSING_CLOCK` when `timeToLive` comes without `clock`, or `INVALID_TIME_TO_LIVE` when `timeToLive` is not a positive finite number.

## Example

```ts
const cache = leastRecentlyUsedCache<string, number>({ capacity: 2 })
cache.set('a', 1)
cache.set('b', 2)
cache.get('a')
cache.set('c', 3) // [{ key: 'b', value: 2 }]
```
