npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ceych

v4.0.0

Published

Wraps any asynchronous function and provides caching of the result

Downloads

38

Readme

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('catbox').Client;
const Redis = require('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.