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

lru-weak-cache

v0.3.3

Published

A caching Map<string, V extends object> that deletes the least-recently-used items with weak references and async generation. Inspired by [lru-cache](https://www.npmjs.com/package/lru-cache).

Downloads

6

Readme

Package Version Build Status Coverage Status Apache License 2.0

node-lru-weak-cache

A caching Map<string, V extends object> that deletes the least-recently-used items with weak references and async generation. Inspired by lru-cache.

npm install lru-weak-cache
export = class LRUWeakCache<V extends object> extends Map<string, V> {
  /**
   * Construct a new LRUWeakCache instance.
   *
   * @param options A number specifying the capacity, or a object of options, if nothing is provided, a capacity of 200 is used by default
   * @param options.minAge The minimum time in milliseconds an item can exist before being allowed to be garbage collected
   * @param options.maxAge The maximum time in milliseconds an object can exist before being erased, this should be higher than minAge or minAge will have no affect
   * @param options.capacity The maximum number of items this cache can contain before it starts erasing old ones
   * @param options.resetTimersOnAccess Whether or not to reset the minAge and maxAge timers when an item is accessed
   */
  constructor(options: number | {minAge?:number,maxAge?:number,capacity?:number,resetTimersOnAccess?:boolean});
  /**
   * Asynchroniously generate a value for a given key with a callback.
   * This method can be called multiple times, or in conjunction with {@see generateMulti} and only calls the generator once per key for the specified caching settings.
   *
   * @param key The key to use
   * @param generator The generator to generate the value using
   * @param callback The callback to call when finished
   */
  generate(key: string, generator: CacheGenerator<V>, callback: (err: Error, value?: V) => void): Cancel<V>;
  /**
   * Asynchroniously generate multiple values for a given key with a callback.
   * This method can be called multiple times, or in conjunction with {@see generate} and only calls the generator once per key for the specified caching settings.
   *
   * @param keys The keys to use
   * @param generator The generator to generate the values using
   * @param callback The callback to call when finished
   */
  generateMulti(keys: string[], generator: CacheMultiGenerator<V>, callback: (err: Error, ret?: {[key: string]: V}) => void): Cancel<{[index:string]:V}>;
  /**
   * Efficiently set multiple values while maintaining the capacity and other settings
   */
  setMulti(data: {[index: string]: V}): this;
  /**
   * Trim least-recently-used items from this map.
   *
   * @param by The amount to trim by
   */
   trim(by: number): this;
}

interface VCancel {
  (): void
}
interface Cancel<V> {
  (dataOrError?: V | Error): void
}
interface CacheGenerator<V extends object> {
  (key: string, callback: (err: Error, value?: V) => void): VCancel | undefined;
}
interface CacheMultiGenerator<V extends object> {
  (keys: string[], callback: (err: Error, ret?: {[key: string]: V}) => void): VCancel | undefined;
}

usage

import LRUWeakCache = require("lru-weak-cache");

var cache = new LRUWeakCache<any>(); // Defaults to a capacity of 200
cache = new LRUWeakCache<any>({maxAge:3600000}); // Nothing older than 1 hour
cache = new LRUWeakCache<any>({minAge:600000}); // 10 minutes before garbage collection
cache = new LRUWeakCache<any>(800); // Capacity of 800

// Everything from Map<string, ?> is implemented

legal

node-lru-weak-cache is licensed under Apache License 2.0