Code Monkey home page Code Monkey logo

cache-eight's Introduction

cache-eight

cache + v8

  • checksum keys

  • access data from json files (persistent cache)

  • optimize functions execution time for expensive operations

  • data is saved using v8's serialize / deserialize

why?

caching expensive pure functions results

same inputs -> same results

use case

from 3s to 1s

@todo

Usage

import Cache from 'cache-eight';

const cache = new Cache();
folder                           file
> OS_temp_dir/.cache8/appName -> cache.json

high level api

const values = [1, 2, 3];
const key = values.toString();

const cached = await cache.get(key, () => values.map(value => value * 2));
// will return the cached if valid or the actual value (and serialize for the future)

custom options:

const customPath = path.resolve(process.cwd());

//custom folder:
const customCache = new Cache(customPath);

//custom file:
customCache.get(k, () => v, { filePath: 'my-file.json' });

// cache duration (time to live):
customCache.get(k, () => v, { ttl: 1000 * 60 }); // number in ms;

granular control api

const values = [1, 2, 3];
const key = values.toString();

async function get() {
    const cached = await cache.deserialize(key);

    // if has cached value, return early (skipping further functions)
    if (cached) return cached;

    //else get it from somewhere
    const result = values.map(value => value * 2);

    //cache the result
    await cache.serialize(key, result);

    return result;
}

await get();

API

get(key, () => val, options?) => val
serialize(key, val, options?) => val
deserialize(key, options?) => cachedVal | false

// key and val are any valid Json objects


options = {
    filPath: string + .json //file.json
    ttl: number //ms
}

cache-eight's People

Contributors

andrew-colman avatar

cache-eight's Issues

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.