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

@zero65tech/cache

v4.0.2

Published

Simple In-memory Cache

Downloads

98

Readme

Cache Utils

This repository provides a set of utility functions for implementing caching mechanisms, including Time-To-Live (TTL) and Least Recently Used (LRU) caches. These utilities can be useful for optimizing the performance of functions by caching their results and minimizing redundant computations.

Installation

npm install @zero65tech/cache

Usage

TTL Cache

The TTL cache implements a simple key-value store with a time-to-live mechanism. The stored values are automatically evicted after a specified duration.

const { ttl } = require('@zero65tech/cache');

const cache = new ttl(60); // Set TTL to 60 seconds

cache.put('key', 'value');
const result = cache.get('key');
console.log(result); // Output: 'value'

LRU Cache

The LRU cache implements a least recently used algorithm to evict the least recently accessed items when the cache size exceeds a specified limit.

const { lru } = require('@zero65tech/cache');

const cache = new lru(2); // Set cache size to 2

cache.put('key1', 'value1');
cache.put('key2', 'value2');
cache.put('key3', 'value3'); // Evicts 'key1' due to size limit

console.log(cache.get('key1')); // Output: undefined
console.log(cache.get('key2')); // Output: 'value2'
console.log(cache.get('key3')); // Output: 'value3'

Function Caching

The repository also provides utility functions for caching the results of asynchronous and synchronous functions.

Asynchronous Function Caching

const { fn, ttl } = require('@zero65tech/cache');
const cache = new ttl();

const asyncFunction = async (param) => {
  // ... some asynchronous computation
  return result;
};

const cachedAsyncFunction = fn(asyncFunction, cache, (param) => param);

const result = await cachedAsyncFunction('someParam');

Synchronous Function Caching

const { fnSync, lru } = require('@zero65tech/cache');
const cache = new lru();

const syncFunction = (param) => {
  // ... some synchronous computation
  return result;
};

const cachedSyncFunction = fnSync(syncFunction, cache, (param) => param);

const result = cachedSyncFunction('someParam');