This plugin provides a cache system for Orama. The cache system is based on async-cache-dedupe.
# You can install Orama using `npm`, `yarn`, `pnpm`:
npm install orama-cache
Example using the default storage (memory)
import { create, insert } from "@orama/orama"
import { createOramaCache } from "orama-cache"
(async() => {
const db = create({ schema: { name: "string" } })
await insert(db, { name: "foo" })
await insert(db, { name: "bar" })
const cache = await createOramaCache(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.
})()
You can use the same APIs as async-cache-dedupe.
Example using Redis as storage
const oramaCache = createOramaCache({
storage: {
type: 'redis',
options: {
client: new Redis(),
invalidation: {
invalidates: true,
referencesTTL: 60 // seconds
}
}
}
})
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
}
}
The orama-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 ║
╟──────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢
║ Orama search │ 30000 │ 184233.47 op/sec │ ± 1.53 % │ ║
╟──────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢
║ Fastest test │ Samples │ Result │ Tolerance │ Difference with slowest ║
╟──────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢
║ Orama caching search │ 30000 │ 596449.98 op/sec │ ± 1.91 % │ + 223.75 % ║
╚══════════════════════╧═════════╧══════════════════╧═══════════╧═════════════════════════╝
╔══════════════════════════════════════╤═════════╤══════════════════╤═══════════╤═════════════════════════╗
║ Slower tests │ Samples │ Result │ Tolerance │ Difference with slowest ║
╟──────────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢
║ Orama search with properties │ 30000 │ 208403.59 op/sec │ ± 1.40 % │ ║
╟──────────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢
║ Fastest test │ Samples │ Result │ Tolerance │ Difference with slowest ║
╟──────────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢
║ Orama caching search with properties │ 30000 │ 524996.42 op/sec │ ± 2.40 % │ + 151.91 % ║
╚══════════════════════════════════════╧═════════╧══════════════════╧═══════════╧═════════════════════════╝
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 test