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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@satoshibits/cache-metrics

v1.0.1

Published

Zero-dependency cache performance monitoring with event-based collection and multiple output formats

Readme

@satoshibits/cache-metrics

Zero-dependency cache performance monitoring with event-based collection and multiple output formats.

Features

  • Zero Dependencies: Pure TypeScript implementation with no external dependencies
  • Event-Based Architecture: Built on a lightweight EventEmitter for flexible handling
  • Multiple Output Formats: Console, Prometheus, JSON, and NDJSON handlers included
  • Time-Window Aggregation: Aggregate metrics over configurable time windows
  • Flexible Handlers: Easy to extend with custom handlers
  • Performance Tracking: Track hit rates, latencies, errors, and cache stampede prevention

Installation

npm install @satoshibits/cache-metrics
# or
pnpm add @satoshibits/cache-metrics

Quick Start

import { CacheCollector } from '@satoshibits/cache-metrics';

// Create a metrics collector
const collector = new CacheCollector({
  maxLatencySamples: 1000,
  calculatePercentiles: true,
});

// Record cache operations
collector.recordHit('user:123', 1.5); // key, latency in ms
collector.recordMiss('user:456', 0.8);
collector.recordSet('user:789', 2.1);
collector.recordDelete('user:123', 0.5);

// Get snapshot
const snapshot = collector.getSnapshot();
console.log(`Hit rate: ${(snapshot.hitRate * 100).toFixed(2)}%`);

With Handlers

import { CacheCollector, ConsoleHandler, PrometheusHandler } from '@satoshibits/cache-metrics';

// Create collector with handlers
const collector = new CacheCollector({
  enableConsole: true,
  enablePrometheus: true,
  snapshotInterval: 60000, // emit snapshots every minute
});

// Use the collector
collector.recordHit('key', 1.2);

Custom Event Handlers

import { CacheCollector, ConsoleHandler } from '@satoshibits/cache-metrics';

const collector = new CacheCollector();

// Add console handler for errors only
const errorHandler = new ConsoleHandler({
  eventTypes: new Set(['error']),
  includeTimestamp: true,
});

collector.on('event', (event) => errorHandler.handle(event));

Prometheus Integration

import { CacheCollector, PrometheusHandler } from '@satoshibits/cache-metrics';

const collector = new CacheCollector();
const prometheusHandler = new PrometheusHandler({
  // Send to your metrics endpoint
  fetch('/metrics', { method: 'POST', body: text });
});

// Emit Prometheus format every 30 seconds
collector.on('snapshot', (snapshot) => prometheusHandler.handle(snapshot));
collector.startSnapshotTimer(30000);

Aggregated Metrics

import { CacheCollector } from '@satoshibits/cache-metrics';

const collector = new CacheCollector();
const aggregator = new MetricsAggregator({
  maxSnapshots: 60,
  windowDuration: 3600000, // 1 hour
});

// Collect snapshots
collector.on('snapshot', (snapshot) => aggregator.addSnapshot(snapshot));
collector.startSnapshotTimer(60000); // every minute

// Get aggregated metrics
const aggregated = aggregator.getAggregatedMetrics();
const hitRateStats = aggregator.getHitRateStats();
console.log(`Average hit rate: ${(hitRateStats.average * 100).toFixed(2)}%`);
console.log(`Trend: ${hitRateStats.trend}`);

API Reference

CacheCollector

The main class for collecting cache metrics.

Methods:

  • record(event) - Record a cache event
  • recordHit(key, latency?) - Record a cache hit
  • recordMiss(key, latency?) - Record a cache miss
  • recordSet(key, latency?) - Record a set operation
  • recordDelete(key, latency?) - Record a delete operation
  • recordError(operation, error?) - Record an error
  • recordStampedePrevented() - Record a prevented cache stampede
  • updateCacheSize(size) - Update cache size metric
  • getSnapshot() - Get current metrics snapshot
  • reset() - Reset all metrics
  • startSnapshotTimer(intervalMs) - Start periodic snapshot emission
  • stopSnapshotTimer() - Stop periodic snapshots

Event Types

  • hit - Cache hit
  • miss - Cache miss
  • set - Set operation
  • delete - Delete operation
  • error - Error occurred
  • clear - Cache cleared

Handlers

  • ConsoleEventHandler - Log events to console
  • ConsoleSnapshotHandler - Log snapshots to console
  • PrometheusHandler - Format metrics in Prometheus exposition format
  • JsonHandler - Format metrics as JSON
  • NdjsonHandler - Stream metrics as newline-delimited JSON

License

ISC