Giter Club home page Giter Club logo

ceych's Introduction

ceych

Wraps any asynchronous function and provides caching of the result

Installation

npm install --save ceych

Usage

'use strict';

const request = require('request');
const Catbox = require('@hapi/catbox').Client;
const Redis = require('@hapi/catbox-redis');

const ceych = require('ceych').createClient({
  cacheClient: new Catbox(new Redis({
    port: 6379,
    host: '127.0.0.1',
    partition: 'cache'
  })),
  defaultTTL: 30
});

function loadData(cb) {
  request.get('https://www.big-data.com/datum.csv', cb);
}

const loadDataCached = ceych.wrap(loadData);

const miss = loadDataCached(); // returned from the server and stored in the cache
const hit = loadDataCached(); // returned from the server and stored in the cache

How does it work?

Ceych automatically creates cache keys based on the wrapped function's body and the arguments passed. This saves you from having to create a unique cache key every time you want the result of a function to be cached.

Return values and arguments need to be serializable to/from JSON. This means that while strings, numbers and basic objects are supported, objects with custom constructors or prototypes are not.

StatsD integration

When using a node-statsd client, ceych will increment a counter each time there is a cache hit or miss. The following metrics are sent:

Metric Type Description
ceych.hits counter Incremented whenever there is a cache hit
ceych.misses counter Incremented whenever there is a cache miss

API

Ceych.createClient(opts)

Creates a ceych client.

Parameters
  • cacheClient - optional - A Catbox client (defaults to an in-memory client).
  • defaultTTL - optional - The default TTL for caching in seconds (default 30).
  • statsClient - optional - An instance of the node-statsd client

ceych.wrap(fn, ttl, suffix)

Returns a wrapped function that implements caching.

Parameters
  • fn - An asynchronous function to be wrapped.
  • ttl - optional - Overrides the default TTL.
  • suffix - optional - A string appended to cache keys to differentiate between identical functions.

ceych.disableCache()

Disables the cache client to allow for wrapped methods to be tested as normal.

ceych's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ceych's Issues

Cache events

Add support for cache events:

ceych.on('hit', () => {
  // eg execute stats client
});

Docs

  • Rollout jsdocs
  • use github pages
  • travis
  • test coverage report

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.