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

cachemate

v1.0.0

Published

A simple in-memory cache with TTL and eviction

Downloads

79

Readme

CacheMate

CacheMate is a simple in-memory cache implementation for Node.js with support for TTL (Time-to-Live) expiration and automatic eviction of the oldest entries when the cache exceeds its maximum size. It also provides cache statistics.

Features

  • TTL Expiration: Automatically expires cache entries based on a specified TTL.
  • Eviction: Evicts the oldest entries when the cache size exceeds the maximum limit.
  • Statistics: Provides hit, miss, eviction, and set counts for monitoring cache usage.
  • Automatic Eviction Process: Periodically checks for and removes expired cache entries.

Installation

Install the package via npm:

npm install cachemate

Usage

Basic Example

const CacheMate = require('cachemate');

// Create a new cache with a maximum size of 2
const cache = new CacheMate(2);

// Set cache entries with TTL of 100ms
cache.set('key1', 'value1', 100);
cache.set('key2', 'value2', 100);

// Retrieve cache entries
cache.get('key1').then(value => console.log(value)); // Outputs: 'value1'

// Wait for TTL to expire and retrieve entries again
setTimeout(() => {
    cache.get('key1').then(value => console.log(value)); // Outputs: null (expired)
    cache.get('key2').then(value => console.log(value)); // Outputs: null (expired)
}, 150);

Handling Eviction

const CacheMate = require('cachemate');

// Create a new cache with a maximum size of 2
const cache = new CacheMate(2);

// Set cache entries
cache.set('key1', 'value1', 100);
cache.set('key2', 'value2', 100);

// Add a third entry to trigger eviction of the oldest entry ('key1')
cache.set('key3', 'value3', 100);

// Retrieve cache entries
cache.get('key1').then(value => console.log(value)); // Outputs: null (evicted)
cache.get('key2').then(value => console.log(value)); // Outputs: 'value2'
cache.get('key3').then(value => console.log(value)); // Outputs: 'value3'

Clearing the Cache

const CacheMate = require('cachemate');

// Create a new cache with a maximum size of 2
const cache = new CacheMate(2);

// Set cache entries
cache.set('key1', 'value1', 100);
cache.set('key2', 'value2', 100);

// Clear the cache
cache.clear();

// Retrieve cache entries after clearing
cache.get('key1').then(value => console.log(value)); // Outputs: null (cleared)
cache.get('key2').then(value => console.log(value)); // Outputs: null (cleared)

// Check cache statistics
const stats = cache.getStats();
console.log(stats); // Outputs: { hits: 0, misses: 2, evictions: 0, sets: 0 }

Cache Statistics

const CacheMate = require('cachemate');

// Create a new cache with a maximum size of 2
const cache = new CacheMate(2);

// Set cache entries
cache.set('key1', 'value1', 100);
cache.set('key2', 'value2', 100);

// Retrieve some cache entries
cache.get('key1');
cache.get('key2');

// Check cache statistics
const stats = cache.getStats();
console.log(stats);
// Example Output: { hits: 2, misses: 0, evictions: 0, sets: 2 }

Methods

  • constructor(maxSize): Creates a new CacheMate instance with a specified maximum size for the cache. Default is 100.
  • async get(key): Retrieves the value associated with key. Returns null if the key does not exist or has expired.
  • async set(key, value, ttl): Sets the value for key with a specified TTL (time-to-live) in milliseconds.
  • evict(key): Manually evicts the specified key from the cache.
  • clear(): Clears all cache entries and resets statistics.
  • getStats(): Returns an object containing cache statistics (hits, misses, evictions, sets).
  • log(message): Logs a message to the console (useful for debugging).

Notes

  • The cache performs automatic eviction of expired entries every second.
  • The eviction process is restarted whenever the cache is cleared.