# lyra-cache

> Implement the cache dedupe on your Lyra instances

Latest version **0.4.0** (published 2023-03-10) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install lyra-cache
pnpm add lyra-cache
yarn add lyra-cache
bun add lyra-cache
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.4.0 |
| Published | 2023-03-10 |
| First published | 2023-01-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 617.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 14 |
| Author | Mateo Nunez |
| Maintainers | mateonunez |
| Keywords | lyra, plugin, caching |

## Links

- npm: https://www.npmjs.com/package/lyra-cache
- Repository: https://github.com/mateonunez/lyra-cache
- Homepage: https://github.com/mateonunez/lyra-cache#readme
- Issues: https://github.com/mateonunez/lyra-cache/issues
- npm.io page: https://npm.io/package/lyra-cache

## Dependencies (2)

- [@lyrasearch/lyra](https://npm.io/package/@lyrasearch/lyra.md) ^0.4.6
- [async-cache-dedupe](https://npm.io/package/async-cache-dedupe.md) ^1.9.0

## Recent versions

- 0.4.0 (latest) — 2023-03-10
- 0.3.1 — 2023-01-20
- 0.3.0 — 2023-01-20

## README

# ✨💨 Lyra Cache

This plugin provides a cache system for [Lyra](https://github.com/lyrasearch/lyra). The cache system is based on [async-cache-dedupe](https://github.com/mcollina/async-cache-dedupe).

[![Tests](https://github.com/mateonunez/lyra-cache/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/mateonunez/lyra-cache/actions/workflows/ci.yml)

## 🚀 Getting Started

### 📦 Installation

```bash
# You can install Lyra using `npm`, `yarn`, `pnpm`:
npm install lyra-cache
```

### 📝 Usage

> Example using the default storage (memory)

```js
import { create, insert } from "@lyrasearch/lyra"
import { createLyraCache } from "lyra-cache"

(async() => {
  const db = create({ schema: { name: "string" } })

  await insert(db, { name: "foo" })
  await insert(db, { name: "bar" })

  const cache = await createLyraCache(db) // Create the cache.

  const results = await cache.search({ term: "foo" }) // This method will return the results and cache them.

  // ...

  const cachedResults = await cache.search({ term: "foo" }) // Returns the cached results.
})()

```

## 📖 Documentation

You can use the same APIs as [async-cache-dedupe](https://github.com/mcollina/async-cache-dedupe#api).

> Example using Redis as storage
```js
const lyraCache = createLyraCache({
  storage: {
    type: 'redis',
    options: {
      client: new Redis(),
      invalidation: {
        invalidates: true,
        referencesTTL: 60 // seconds
      }
    }
  }
})
```

## 📈 Benchmarks

Some searches can be ~2K faster using a cache system.

```js
{
  elapsedTime: '81ms',
  fetchRes: {
    elapsed: 81312667n,
    hits: [
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object]
    ],
    count: 100000
  }
}
// Cached result, same searched `term`
{
  elapsedTimeCached: '40μs',
  fetchCached: {
    elapsed: 81312667n,
    hits: [
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object]
    ],
    count: 100000
  }
}
```

### ✅ Results

The `lyra-cache` plugin provides a set of benchmarks to compare the performance of the cache system with the default search.

```bash
npm run benchmark
```

```bash
# Results

╔═════════════════════╤═════════╤══════════════════╤═══════════╤═════════════════════════╗
║ Slower tests        │ Samples │           Result │ Tolerance │ Difference with slowest ║
╟─────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢
║ Lyra search         │   30000 │ 365541.89 op/sec │  ± 3.86 % │                         ║
╟─────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢
║ Fastest test        │ Samples │           Result │ Tolerance │ Difference with slowest ║
╟─────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢
║ Lyra caching search │   30000 │ 654465.42 op/sec │  ± 1.38 % │ + 79.04 %               ║
╚═════════════════════╧═════════╧══════════════════╧═══════════╧═════════════════════════╝

╔═════════════════════════════════════╤═════════╤══════════════════╤═══════════╤═════════════════════════╗
║ Slower tests                        │ Samples │           Result │ Tolerance │ Difference with slowest ║
╟─────────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢
║ Lyra search with properties         │   30000 │ 382326.49 op/sec │  ± 1.73 % │                         ║
╟─────────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢
║ Fastest test                        │ Samples │           Result │ Tolerance │ Difference with slowest ║
╟─────────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢
║ Lyra caching search with properties │   30000 │ 668725.79 op/sec │  ± 1.77 % │ + 74.91 %               ║
╚═════════════════════════════════════╧═════════╧══════════════════╧═══════════╧═════════════════════════╝
```

## ⚠️ Testing

To run the tests you should run the following commands:

```bash
npm run redis
```

That command shall start a new Redis istance using Docker. Then run the tests.

```bash
npm run tests
```

## 📝 License

[MIT](/LICENSE)

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