# CacheEvictionPolicy

Defined in: [packages/cache/src/cache.ts:276](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/cache/src/cache.ts#L276)

A caller-supplied eviction policy: the order in which a `customCache` gives up its keys.

The cache owns every entry, weight, expiry, and validation; the policy sees keys only and owns only the order it will evict them in. The cache tells it when a key becomes resident, is used, or leaves, and asks it for a victim when a `set` needs room. `peek` and `has` never report a use; like every read, they report `deleted` when they drop an entry they find expired.

Every callback runs synchronously inside a cache member, and none may call back into that cache: such a call throws `CacheInvalidInputError` with code `REENTRANT_POLICY`. Reading `size` and `totalWeight` is allowed. An error a callback throws propagates out of the cache member that called it; `customCache` documents what each one leaves behind.

## Type Parameters

### K

`K`

## Properties

### added

> `readonly` **added**: (`key`) => `void`

Defined in: [packages/cache/src/cache.ts:278](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/cache/src/cache.ts#L278)

A key the cache did not hold became resident, through `set`.

#### Parameters

##### key

`K`

#### Returns

`void`

***

### used

> `readonly` **used**: (`key`) => `void`

Defined in: [packages/cache/src/cache.ts:280](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/cache/src/cache.ts#L280)

A resident key was read with `get`, or replaced with `set`: a replacement counts as a use.

#### Parameters

##### key

`K`

#### Returns

`void`

***

### deleted

> `readonly` **deleted**: (`key`) => `void`

Defined in: [packages/cache/src/cache.ts:285](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/cache/src/cache.ts#L285)

A resident key left the cache: deleted, dropped after it expired, or evicted. Not called for `clear`.

#### Parameters

##### key

`K`

#### Returns

`void`

***

### chooseVictim

> `readonly` **chooseVictim**: (`context`) => `K`

Defined in: [packages/cache/src/cache.ts:293](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/cache/src/cache.ts#L293)

Return the resident key to evict next. Called only while the cache holds at least one key other than [CacheVictimContext.protectedKey](/reference/knotkit/cache/interfaces/cachevictimcontext/#protectedkey), and before anything is removed for it. Returning a key the cache does not hold, or the protected key, throws `INVALID_VICTIM` from `set` and leaves that eviction undone. `deleted` follows for the returned key.

#### Parameters

##### context

[`CacheVictimContext`](/reference/knotkit/cache/interfaces/cachevictimcontext/)<`K`>

#### Returns

`K`

***

### clear?

> `readonly` `optional` **clear?**: () => `void`

Defined in: [packages/cache/src/cache.ts:298](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/cache/src/cache.ts#L298)

Forget every key, because the cache was cleared. A policy that keeps any per-key state must provide it: `clear` reports no `deleted` per key.

#### Returns

`void`
