Giter Site home page Giter Site logo

keshi's Introduction

Keshi

Keshi on NPM Keshi on TravisCI

Keshi is a better in-memory cache for Node and the browser.

const createCache = require('keshi')

or

import createCache from 'keshi'

Usage

const cache = createCache()

const user = await cache.resolve('user', () => fetch('https://myapi.com/user'), '30 mins')

What this will do:

  • Fetch the user from the API as it doesn't have it in cache.
  • If called again within 30 minutes it will return the cached user.
  • If called after 30 minutes it will fetch the user again and re-cache.

Cache the data you need

You should return only the data you need to keep the cache efficient. Here's a real world Node example of caching repository information from GitHub:

const fetchProjectMeta = project => got.get(`https://api.github.com/repositories/${project.repo.id}`, {
  json: true,
  headers: {
    Authorization: `token ${project.accessToken}`,
  },
}).then(r => ({ name: r.body.full_name, description: r.body.description })); // Caching name and desc

async function resolveProjectMeta(project) {
  try {
    const meta = await cache.resolve(project.repo.id, fetchProjectMeta(project), '1 hour');
    return { ...project, ...meta };
  } catch (e) {
    console.error(e);
    return project;
  }
}

Among other things caches are ideal when dealing with rate limited external APIs (and saving bandwidth), without the worries of persistant data.

Keshi will automatically keep memory low by cleaning up expired items.

API

resolve(key, [value], [expiresIn])

key → String → Required

value → Any → Optional

A function which resolves to a value, or simply a literal value.

expiresIn → Number | String | Function → Optional

A number in milliseconds or anything that ms accepts after which the value is considered expired. If no expiry is provided the item will never expire.

Can also be a function (async is allowed) which returns true if the item has expired or otherwise false. e.g.

// Expire 50% of the time.
const date = await cache.resolve('date', () => new Date(), () => Math.random() >= 0.5)

del(key)

Delete a cached item by key.

clear()

Clear all cached items.

keshi's People

Contributors

sekoyo avatar

Watchers

 avatar  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.