# @koopjs/cache-memory

> An in-memory cache for KOop

Latest version **6.0.0** (published 2024-02-08) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @koopjs/cache-memory
pnpm add @koopjs/cache-memory
yarn add @koopjs/cache-memory
bun add @koopjs/cache-memory
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 6.0.0 |
| Published | 2024-02-08 |
| First published | 2022-04-15 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 5.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 713 |
| Maintainers | sanshr, drspacemanphd, sonofflynn89, tomtom92, haoliang, richgwozdz, dhatcher |
| Keywords | koop, cache, memory |

## Links

- npm: https://www.npmjs.com/package/@koopjs/cache-memory
- Repository: https://github.com/koopjs/koop
- Homepage: https://github.com/koopjs/koop#readme
- Issues: https://github.com/koopjs/koop/issues
- npm.io page: https://npm.io/package/@koopjs/cache-memory

## Dependencies (2)

- [lodash](https://npm.io/package/lodash.md) ^4.17.21
- [@alloc/quick-lru](https://npm.io/package/@alloc/quick-lru.md) ^5.2.0

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 6.0.0 (latest) — 2024-02-08
- 5.0.0 — 2023-07-12
- 4.0.1 — 2023-04-12
- 4.0.0 — 2023-02-14
- 3.0.1 — 2022-11-27
- 3.0.0 — 2022-11-27
- 2.0.3 — 2022-09-16
- 2.0.2 — 2022-08-05
- 2.0.1 — 2022-08-05
- 2.0.0 — 2022-08-04
- 1.2.0 — 2022-06-23
- 1.1.2 — 2022-04-15
- 1.1.1 — 2022-04-15

## README

# Koop Memory Cache
[![npm version][npm-img]][npm-url]
![coverage](https://raw.githubusercontent.com/koopjs/koop/master/packages/cache-memory/coverage.svg)

This is a LRU cache with ttl (time to live) expiry. It expects cache items to be GeoJSON feature collections. It is based on [quick-lru](https://github.com/sindresorhus/quick-lru). 

Cache eviction occurs in two ways:
1. if the cache grows to a number of items greater than the cache's defined size, the least recently inserted or accessed item is evicted
2. if a item is accessed and it has gone past its ttl period, it is evicted
## Usage

### Stand-alone instantiation:

```js
const Cache = require('@koopjs/cache-memory');
const cache = new Cache({ size: 1000 });
```

### As a Koop cache plugin
This is the default cache for [Koop](https://github.com/koopjs/koop) so you won't need to instantiate it yourself. If you really wanted to, it would look like this:

```js
const Koop = require('koop')
const koop = new Koop()
const cache = require('@koopjs/cache-memory')
koop.register(cache, { size: 1000 })
```
#### Options
`size`: the maximum number of items to store in the queue before evicting the least recently used item.

## Cache API
The cache is a JavaScript object that lives in-memory. It is used to store geojson features.

### `insert`
Insert geojson into the cache.


```js
const geojson = {
  type: 'FeatureCollection',
  features: [],
  metadata: { 
    name: 'Example GeoJSON',
    description: 'This is geojson that will be stored in the cache'
  }
}

const options = {
  ttl: 1000 // The TTL option is measured in seconds, it will be used to set the `maxAge` property in the LRU cache
}

cache.insert('key', geojson, options, err => {
  // This function will call back with an error if one occurs
})
```

### `retrieve`
Retrieve a cached feature collection.

```js
const options = {
  pick: [] // an array of keys used to return a subset of the feature collections root level properties
  omit
} 

cache.retrieve('key', options, (err, geojson) => {
  /* This function will callback with an error or the data cached with the passed key. It will return undefined if not found or expired.
  {
    type: 'FeatureCollection',
    features: [],
    metadata: {}
  }
  */
})
```

### `delete`
Remove a feature collection from the cache

```js
cache.delete('key', err => {
  // This function will call back with an error if one occurs
})
```

[npm-img]: https://img.shields.io/npm/v/@koopjs/cache-memory.svg?style=flat-square
[npm-url]: https://www.npmjs.com/package/@koopjs/cache-memory

---
_Source: https://npm.io/package/@koopjs/cache-memory · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
