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-clock

v1.6.0

Published

An in-memory cache clock with TTL based expiry built for NodeJS and the browser.

Downloads

4

Readme

cache-clock

GitHub Workflow Status GitHub package.json version GitHub

A TypeScript implementation of a cache clock with TTL based expiry, driven by a single setTimeout call. Provides simple methods for setting, getting, and deleting cache entries with a chainable API design. By default, the cache is checked every 15 seconds for expired entries.

Table of Contents

Features

  • Supports TypeScript
  • Built for NodeJS and the browser
  • Lightweight and fast (4kb gzipped)
  • Built-in automatic cache expiry
  • Entries are stored in a Map for fast retrieval
  • Item keys are hashed for fast lookup
  • Cache entry statistics

Installation

npm:

$ npm install cache-clock

yarn:

$ yarn add cache-clock

Usage

CommonJS:

const { CacheClock } = require('cache-clock');

// or

const CacheClock = require('cache-clock').CacheClock;

const cache = new CacheClock(config);

ES6:

import { CacheClock } from 'cache-clock';

const cache = new CacheClock(config);

Note: When intializing a new cache clock, the clock will start immediately. You can opt to not start the clock by passing either passing { autoStart: false } or calling the .stop() method.

Examples

Basic

import { CacheClock } from 'cache-clock';

const cache = new CacheClock();

cache.set('foo', 'bar');

console.log(cache.get('foo')); // bar

With TTL

const cache = new CacheClock({
    ttl: 10000, // 10 seconds
});

cache.set('foo', 'bar');

// or

const cache = new CacheClock();

cache.set('foo', 'bar', { ttl: 10000 }); // 10 seconds

setTimeout(() => {
    console.log(cache.get('foo')); // undefined
}, 11000);

TTL Overwrite

const cache = new CacheClock({
    ttl: 10000, // 10 seconds
});

cache.set('foo', 'bar', { ttl: 5000 }); // 5 seconds

API

const cache = new CacheClock(options);

// or

const cache = CacheClock.create(options);

age

Returns the age of the cache in milliseconds.

cache.age;

size

Returns the current size of the cache.

cache.size;

options

An object representing the current options of the cache.

cache.options;

isRunning

Returns a boolean indicating if the cache clock is running.

cache.isRunning;

configure([, options])

Update the cache clock configuration. Use this method to update the cache clock configuration after the clock has been initialized. Any items in the cache prior to calling this method will not be affected. It is recommended to instead pass these options when initializing the constructor.

options

| Option | Default | Description | |----------------------|----------|---------------------------------------------------------------------------------------------------------------------------------| | maxItems | 1000 | The maximum number of items to store in the cache at once. Exceeding this limit will remove the oldest entry. | | ttl | Infinity | The time to live for all entries in the cache. | | interval | 15000 | The interval in ms to check for expired items. It is recommended to keep this value above 15 seconds for optimal performance. | | onExpire | null | A function to call when an item has expired. | | overwrite | false | Whether to overwrite existing entries. | | resetTimeoutOnAccess | false | When accessing entries via get or has if the expiration should be reset. | | autoStart | true | Programmatically determine if you wish for the clock to auto start. | | debug | false | Log debug messages to the console. Includes success, warning and error messages. | |

Note: When passing either Infinity or 0 as the interval, this disables the internal clock. If a clock has already started, once it has finished its current cycle, it will stop.

To preserve false positives when resetTimeoutOnAccess is true, the expiration is checked before the timeout is reset. The expiration is also relative to the current time.

start()

Start the cache clock. This is automatically called when the cache clock is created. You should only need to call this method if you have stopped the cache clock manually.

This will spawn a new clock with the full timeout interval. This does not resume the lock from where it left off.

stop()

Manually stop the cache clock from running. This will disable the automatic expiration of entries. This does not prevent items from being checked for expiration when using the .get() or .has() method.

getCacheKey(input)

Create a cache key from the input. This is used internally to create a hash of the key for fast lookup.

set(key, value[, options])

cache.set('foo', 'bar', { ttl: 20000 }); // CacheEntry object is returned

When adding new entries, the cache is checked for overflow. If the cache is full, the oldest entry will be removed to make room for the new entry.

options

| Option | Default | |----------------------|----------| | overwrite | false | | ttl | false |

get(key)

Get a cache entry by key. If the entry is not found, undefined will be returned. If the entry is found, the entry is checked for expiration and removed if expired. Returns the full entry from the cache.

const entry = cache.get('foo');

// {
//     k: "foo",
//     v: "bar;
//     t: 20000;
//     e: 1611234567890;
// }
  • k - The hashed key of the entry
  • v - The value of the entry
  • t - The time to live of the entry
  • e - The expiry time of the entry

del(key)

Delete a cache entry by key. If the entry is not found, undefined will be returned. If the entry is found, the entry is removed from the cache and returned.

const entry = cache.del('foo');

// {
//     k: "foo",
//     v: "bar;
//     t: 20000;
//     e: 1611234567890;
// }

has(key)

Check if a cache entry exists by key. If the entry is not found, false will be returned. If the entry is found, the entry is checked for expiration and removed if expired. Returns true if the entry exists.

clear()

Clear all entries from the cache.

toJSON()

Returns a JSON representation of the cache.

Cache Statistics

The cache module now supports internal counters so you can visualise the cache usage. This is useful for debugging and monitoring.

All counters are made available via cache.stats accessor.

export interface CacheStatistics {
    /**
     * The number of times the cache was accessed.
     */
    hits: number;
    /**
     * The number of items that were added to the cache.
     */
    sets: number;
    /**
     * The number of times the cache was accessed and
     * no item was found.
     */
    misses: number;
    /**
     * The number of items that were removed from the
     * cache due to `maxItems` overflowing.
     */
    evictions: number;
    /**
     * The number of items that were removed from the
     * cache due to expiration.
     */
    expired: number;
    /**
     * The number of items that were deleted from the
     * cache.
     */
    deletes: number;
    /**
     * The number of items that were overwritten.
     */
    overwrites: number;
    /**
     * The number of times the cache was cleared.
     */
    clears: number;
    /**
     * The number of life-cycle events that were triggered.
     */
    lifecycles: number;
}

Changelog

See CHANGELOG.md

License

MIT