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

@extensionengine/tapster

v1.0.1

Published

Cache adapter module for NodeJs

Downloads

5

Readme

Tapster

Cache adapter module for NodeJs.

Installation:

npm install @extensionengine/tapster
const { CacheManager } = require('@extensionengine/tapster');
const cache = new CacheManager({ /* ... */ });

Store providers

  • memory (uses LRU)
  • redis (uses ioredis)
  • custom - use any store you want, as long as it has the same API

Usage

See examples below and in the examples directory.

Memory store

const client = new CacheManager({ store: 'memory', ttl: 10 /* seconds */ });

// If the set method is called without ttl, the default ttl will be used
await client.set('foo', 'bar');
await client.get('foo'); // bar
await client.has('foo') // true
await client.has('baz'); // false

await sleep('10s');
await client.get('foo'); // undefined
await client.has('foo') // false

// When ttl is defined, it will overwrite the default one
await client.set('foo', 'bar', 5);
await client.has('foo') // true

await sleep('5s');
await client.has('foo') // false

// ttl = 0 means no expiration time
await client.set('foo', 'bar', 0);

Redis store

const client = new CacheManager({
  store: 'redis',
  host: 'localhost',
  port: 6379,
  ttl: 10 /* seconds */
});

await client.set('foo', 'bar');
await client.get('foo'); // bar

Custom store

You can use your own custom store by creating one with the same API as the built-in memory stores (such as a memory or redis). See example.

class CustomStore { /* ... */ }
const client = new CacheManager({ store: CustomStore });

await client.set('foo', 'bar');
await client.get('foo'); // bar

Serialization

Tapster uses JSON.stringify and JSON.parse for data serialization. You can optionally provide your own serialization functions to support extra data types or to serialize to something other than JSON.

const cache = new CacheManager({ serialize: JSON.stringify, deserialize: JSON.parse });

Namespaces

Namespacing cache instance enables avoiding key collisions and allows clearing only a certain namespace while using the same database.

const users = new CacheManager({ namespace: 'users' });
const cars = new CacheManager({ namespace: 'cars' });

await users.set('record-1', 'John');
await cars.set('record-1', 'Honda');

console.log('User keys: ', await users.getKeys()); // ['record-1'];
console.log(await users.get('record-1')); // John
console.log('Car keys: ', await cars.getKeys()); // ['record-1']
console.log(await cars.get('record-1')); // Honda

await users.clear();

console.log('User keys: ', await users.getKeys()); // []
console.log(await users.get('record-1')); // undefined
console.log('Car keys: ', await cars.getKeys()); // ['record-1']
console.log(await cars.get('record-1')); // Honda

Options

Common

  • store (optional) - built-in store (memory, redis) or custom store. Default is memory.
  • ttl (optional) - time to live in seconds. Default is 0.
  • namespace (optional) - namespace cache instance to avoid key collisions. Default is default.

Redis store

  • host (required) - redis host.
  • port (required) - redis port.
  • password (optional) - redis password.

API

  • set(key, value, ttl) - TTL is optional. The cache manager instance's TTL will be used if the set method is called without a ttl parameter.
  • get(key) => value
  • delete(key)
  • has(key)
  • clear - Clear all records under the certain namespace
  • getKeys(pattern) => keys

Supported glob-style patterns:

  • h?llo matches hello, hallo and hxllo
  • h*llo matches hllo and heeeello
  • h[ae]llo matches hello and hallo, but not hillo
  • h[^e]llo matches hallo, hbllo, ... but not hello
  • h[a-b]llo matches hallo and hbllo

Tests

To run tests run:

npm t

License

Tapster is licensed under the MIT license.