hybrid-disk-cache
A hybrid disk cache library that utilized both the solid SQLite3 database and the file system.
npm i hybrid-disk-cache
When the value is larger than 10 kilobytes, it will be written to the file system, otherwise saved in SQLite3 database.
The benefits of using this kind of hybrid cache are:
- Always use the small footprint and high performace SQLite3 index.
- Using file system for larger files. No need to run
vacuum
for releasing space
Also, here are some bonus:
- 100% test coverage
- Pure Typescript
- Used in production with 300K keys
- SQLite3's indices will always be used when searching for a key. (which is FAST)
This hybrid idea is inspired by python-diskcache
. We used it in our Python production stack, and it works just as great as what we'd expected.
APIs
const cache = new Cache({ path, ttl, tbd })
cache.set(key, value)
cache.set(key, value, 5)
cache.get(key, defaultValue)
cache.del(key)
const status = cache.has(key)
if (cache.has(key) !== 'miss') {
const value = cache.get(key)
}
if (cache.has(key) === 'hit') {
const value = cache.get(key)
}
cache.purge()
Check index.test.ts
for examples.
Benchmarks
With a series of 10B ~ 500KB data writing and reading, here are the results on a Samsung 860 EVO SATA SSD:
Here is the benchmark source code.
> cache located at: /tmp/hdc
> generating bench data from 10B to 500000B
> starting 3000 x 15 writes
done: 69.34 μs/record.
> starting 3000 x 15 reads
done: 26.65 μs/record.
License
MIT. Copyright 2020, Rakuraku Jyo.