0.4.0 โข Published 1 year ago
lyra-cache v0.4.0
โจ๐จ Lyra Cache
This plugin provides a cache system for Lyra. The cache system is based on async-cache-dedupe.
๐ Getting Started
๐ฆ Installation
# You can install Lyra using `npm`, `yarn`, `pnpm`:
npm install lyra-cache
๐ Usage
Example using the default storage (memory)
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.
Example using Redis as storage
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.
{
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.
npm run benchmark
# 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:
npm run redis
That command shall start a new Redis istance using Docker. Then run the tests.
npm run tests