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

ant-cache

v0.0.8

Published

Simply fast in-memory key-value cache that supports TTL for Node.

Downloads

36

Readme

Ant Cache

Simply fast in-memory key-value cache that supports TTL for Node.

Getting started

npm install ant-cache

Use cases

  1. When you don't want (or need) to set up a separate service, like Redis or Memcached, for your local newspaper app.
  2. When you just need a simple cache working like a mediator between your app and your real Backend.
  3. When you just need a simple store for your config.

Usage

With ES Module

import { AntCache } from 'ant-cache';

With CommonJS

const { AntCache } = require('ant-cache');

Initialize

// use default config
const cache = new AntCache();

// initialize with config
const cache = new AntCache({
  ttl: 120,  // in seconds
  checkPeriod: 10,  // in seconds
  maxKeys: 1000,  // could hold maximum 1000 key-value pairs
})

Insert/Update

// set a value with default ttl
cache.set('a number', 12);

// set a value with specific ttl 10 minutes
cache.set('a mighty commit', {
  id: '0efa4d37c3097bca9c58f4eaf75f86e7efdc518a',
  message: 'skyrocket'
}, 600);

// passing in an existing key to update value, `ttl` has no effect
cache.set('a mighty commit', {
  id: '0efa4d37c3097bca9c58f4eaf75f86e7efdc518a',
  message: 'enhanced skyrocket'
}, 600);

// if a value is inserted with `ttl` = 0, it will live permanently unless deleted manually
cache.set('permanent value', 'Linus Torvald', 0);

Get

// get a single key
cache.get('a number');

// get multiple keys, returns an object
cache.getMany('a mighty commit', 'permanent value');

// also accept array(s)
cache.getMany('a number', ['a mighty commit', 'permanent value']);

Delete

// delete a single key
cache.delete('a number');

// delete multiple keys, returns an object
cache.deleteMany('a mighty commit', 'permanent value');

// also accept array(s)
cache.deleteMany('a number', ['a mighty commit', 'permanent value']);

// clear cache
cache.flushAll();

Stats

cache.stats()
/*
{
  hits: 5,
  misses: 2,
  keys: 3
}
*/

Hooks

By default, deleteOnExpire option is true, the cache will delete the expired value automatically. If you want to have your own logic, use expired hook:

cache.on('expired', ({
  key,
  value,
  ttl,
  deleteCurrentKey,
}) => {
  console.info(`%s is expired`, key);
  deleteCurrentKey();
})

AntCache expose several hooks:

  • before-delete
  • after-delete
  • before-set
  • after-set
  • expired

Serialization

AntCache supports serialize and deserialize its content and TTLs. Any types supported by superjson should work as AntCache uses superjson under the hood.

The bigger the cache is, the longer it takes to complete. In worst cases, your app could be halted. So use those methods with caution.

// stringify cache content and TTLs
const json = cache.serialize();

// parse JSON string and overwrite the cache content and TTLs.
cache.deserialize(json);

API reference

Exhaustive API reference and examples:

bonniss.github.io/ant-cache