0.2.0 • Published 1 year ago
@mateonunez/lyra-cache v0.2.0
Lyra cache
Implement the cache dedupe on your Lyra instances.
Installation
You can install Lyra using npm
, yarn
, pnpm
:
npm i @mateonunez/lyra-cache
yarn add @mateonunez/lyra-cache
pnpm add @mateonunez/lyra-cache
Usage
import { create, insert } from "@lyrasearch/lyra"
import { createLyraCache, searchCache } from "@mateonunez/lyra-cache"
(async() => {
const db = create({ schema: { name: "string" } })
await insert(db, { name: "foo" })
await insert(db, { name: "bar" })
const results = await searchCache({ db, term: "foo" }) // native Lyra search
// The next same search will be cached
const lyraCache = await createLyraCache() // create a cache instance
const resultsCached = await searchCache({ db, term: "foo" }, lyraCache) // performed x2000 times faster via cache
})()
The Lyra Cache object
The Lyra Cache object is a async-cache-dedupe wrapper. It means that you can use all the methods that the original package provides. Same for the options.
Example using Redis as storage
const lyraCache = createLyraCache({ storage: { type: 'redis', options: { client: new Redis(), invalidation: { referencesTTL: 60 // seconds } } } })
Example
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
}
}