bbc/ceych

Name: ceych

Owner: BBC

Description: Wraps any asynchronous function and provides caching of the result

Created: 2016-02-12 10:33:03.0

Updated: 2017-03-21 20:22:43.0

Pushed: 2017-08-08 09:59:51.0

Homepage: null

Size: 26

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

ceych

Wraps any asynchronous function and provides caching of the result

Installation
install --save ceych
Usage
 strict';

t request = require('request');
t Catbox = require('catbox').Client;
t Redis = require('catbox-redis');

t ceych = require('ceych').createClient({
cheClient: new Catbox(new Redis({
port: 6379,
host: '127.0.0.1',
partition: 'cache'
),
faultTTL: 30


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


t loadDataCached = ceych.wrap(loadData);

DataCached((err, res) => {
 `res` is returned from the server and stored in the cache

adDataCached((err, res) => {
// `res` is returned from the cache until TTL expiry
;

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 ceych.wrap(fn, ttl, suffix)

Returns a wrapped function that implements caching.

Parameters ceych.disableCache()

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


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.