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

cache-suite

v0.2.1

Published

[![Build Status](https://travis-ci.org/sleagon/cache-suite.svg?branch=master)](https://travis-ci.org/sleagon/cache-suite)

Downloads

4

Readme

Cache Suite

Build Status

Features

In order to keep our system as strong as possible, we may build multi level cache in production environment, such as memory/file system/redis/oss... This package is written to manage this kind of problem. Just add your cacher middleware to the cache chain like koa middleware, then use cache.get/set simply. 😁😁😁

  • Fully typescript supported.
  • Koa-like cache handler.
  • Multi level cacher supported.
  • Simple, Reliable, Flexible.

Usage

Take a look the test cases if you want to know how to use it in typescript project.

Install

npm install cache-suite --save
# or
yarn add cache-suite

Basic Cache

You can init a empty cache using new Cache().

Middleware(caches)

Just like koa, you can add mddleware cache using cache.use(...). Function use need a interface having get property.

export declare interface Handler<T> {
    get: HandlerFunc<T>;
    set?: HandlerFunc<T>;
    del?: HandlerFunc<T>;
}
Cache#use(handler: Handler<T>): this;

Set/Get

Just cache.get(...) or cache.set(...).

Simplest case

const Cache = require('../lib/index.js').default;
const mp = new Map();
const memoryCache = {
  get: async ctx => ctx.body = mp.get(ctx.key),
  set: async ctx => mp.set(ctx.key, ctx.body),
  del: async ctx => mp.delete(ctx.key),
}

const cache = new Cache();
cache.use(memoryCache);

(async function call() {
  await cache.set('foo', 'bar');
  let data = await cache.get('foo');
  console.log('>>>', data);
  // >>> bar
  await cache.del('foo');
  data = await cache.get('foo');
  console.log('>>>', data);
  // >>> undefined
})()

Chained case

const Cache = require('../lib/index.js').default;
const { promisify } = require('util');
const fs = require('fs');

// simple memory cache
class MemoryCache {
  constructor() {
    this.mp = new Map();
  }
  async get(ctx, next) {
    ctx.body = this.mp.get(ctx.key);
    if (ctx.body) {
      ctx.body = '[from MemoryCache]' + ctx.body;
      ctx.source = 'MEMORY';
      return;
    }
    await next();
    if (ctx.body) {
      this.mp.set(ctx.key, ctx.body);
    }
  }
}

// simple file system cache
class FileCache {
  constructor() {
    // make sure /tmp/test exist
    this.baseDir = '/tmp/test';
  }
  async get(ctx, next) {
    ctx.body = await promisify(fs.readFile)(`${this.baseDir}/${ctx.key}`);
    // add some useless info to show the source of data.
    ctx.body = '[from FileCache]' + ctx.body.toString();
    ctx.source = 'FILE';
  }
  async set(ctx, next) {
    await promisify(fs.writeFile)(`${this.baseDir}/${ctx.key}`, ctx.body);
  }
}

const cache = new Cache();
const mcache = new MemoryCache();
const fcache = new FileCache();
cache.use(mcache);
cache.use(fcache);

(async function call() {
  await cache.set('foo', 'bar');
  let data = await cache.get('foo');
  console.log('>>>', data);
  // >>> [from FileCache]bar
  data = await cache.get('foo');
  console.log('>>>', data);
  // memory cached data is from file cache, so [from FileCache] is cached as raw data.
  // >>> [from Memory][from FileCache]bar
})();