GithubHelp home page GithubHelp logo

jedamzik / hybrid-disk-cache Goto Github PK

View Code? Open in Web Editor NEW

This project forked from next-boost/hybrid-disk-cache

0.0 0.0 0.0 715 KB

A hybrid disk cache library that utilized both the solid SQLite3 and file system.

TypeScript 100.00%

hybrid-disk-cache's Introduction

Coverage Status Maintainability

hybrid-disk-cache

A hybrid disk cache library that utilized both the solid SQLite3 database and the file system.

yarn add @next-boost/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

// tbd, time before deletion: This is used to control how long a key
// should remain in the cache after expired (ttl)
// And `cache.purge` will delete all records with ttl + tbd < now
const cache = new Cache({ path, ttl, tbd })

// set. if ttl empty, use the cache's ttl
cache.set(key, value)
// set. will expire in 5 seconds
cache.set(key, value, 5)

// get
cache.get(key, defaultValue)

// del
cache.del(key)

// check cache availability and status
// status in 'miss' | 'stale' | 'hit'
const status = cache.has(key)

// if you want to serve even the stale value
if (cache.has(key) !== 'miss') {
    const value = cache.get(key)
}

// if you only want the unexpired one
if (cache.has(key) === 'hit') {
    const value = cache.get(key)
}

// delete all expired keys
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.

hybrid-disk-cache's People

Contributors

rjyo avatar jedamzik avatar dependabot[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.