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

rainbow-lru-cache

v0.0.4

Published

## How to use LRUCache

Downloads

60

Readme

Cache library

How to use LRUCache

import { LRUCache } from './index'

(async () => {
    const cache = new LRUCache<string>(); // by default use new Map() as storage and 100 of capacity

    await cache.set('1', 'Hello1'); // the item 1 is added and is set as top 
    await cache.set('2', 'Hello2'); // the item 2 is added and is set as top
    await cache.set('3', 'Hello3'); // the item 3 is added and is set as top
    await cache.set('4', 'Hello4'); // the item 4 is added and is set as top
    await cache.set('5', 'Hello5'); // the item 5 is added and is set as top
    await cache.set('6', 'Hello6'); // the item 6 is added and is set as top
    await cache.get('2'); // the item 2 is used and then is set as top
})()

How to use LRUCache specifying a storage memory and capacity

import { LRUCache, IStorage, memoize } from './index'

class InMemoryStorage implements IStorage {
    private __data: Record<string, string | undefined>;
    constructor(){
        this.__data = {};
    }
    delete(key: string): boolean {
        return delete this.__data[key];
    }
    get(key: string): string | undefined {
        return this.__data[key];
    }
    set(key: string, value: string): void {
        this.__data[key]=value;
    }
}

(async () => {
    const cacheStorage = new InMemoryStorage();
    const cache = new LRUCache<string>(cacheStorage, 5);

    await cache.set('1', 'Hello1'); // the item 1 is added and is set as top 
    await cache.set('2', 'Hello2'); // the item 2 is added and is set as top
    await cache.set('3', 'Hello3'); // the item 3 is added and is set as top
    await cache.set('4', 'Hello4'); // the item 4 is added and is set as top
    await cache.set('5', 'Hello5'); // the item 5 is added and is set as top
    await cache.set('6', 'Hello6'); // the item 6 is added and is set as top, the item 1 is deleted
    await cache.get('2'); // the item 2 is used and then is set as top
    await cache.set('7', 'Hello7'); // the item 7 is added and is set as top the item 3 is deleted
    // await cache.print();
    const array = [];
    for await (const [key, value] of cache){
        array.push(key);
    }
    console.log(
        array.reduce(
            ((acc: string, current: string) => {
                return `${acc} > ${current}`
            }) as any
        )
    )
})()

How to use memoize with cache

import {LRUCache, IStorage, memoize } from './index'

class InMemoryStorage implements IStorage {
    private __data: Record<string, string | undefined>;
    constructor(){
        this.__data = {};
    }
    delete(key: string): boolean {
        return delete this.__data[key];
    }
    get(key: string): string | undefined {
        return this.__data[key];
    }
    set(key: string, value: string): void {
        this.__data[key]=value;
    }
}

const sleep = (time: number)=>new Promise(resolve => setTimeout(resolve, time))

const expensiveCalculation = async (arg1: number, arg2: number) => {
    await sleep(2000);
    return arg1 + arg2;
}

(async () => {    
    const cacheStorage = new InMemoryStorage();
    const cache = new LRUCache<string>(cacheStorage, 3);
    const expensiveCalculationMemoized = memoize({
        func: expensiveCalculation,
        cache,
    });

    console.log(await expensiveCalculationMemoized(1, 2)); // calling expensiveCalculation and caching the result
    console.log(await expensiveCalculationMemoized(1, 2)); // using cached result
    console.log(await expensiveCalculationMemoized(1, 3)); // calling expensiveCalculation and caching the result
    console.log(await expensiveCalculationMemoized(1, 3)); // using cached result
    console.log(await expensiveCalculationMemoized(1, 4)); // calling expensiveCalculation and caching the result
    console.log(await expensiveCalculationMemoized(1, 4)); // using cached result
    console.log(await expensiveCalculationMemoized(1, 5)); // calling expensiveCalculation and caching the result, key [1, 2] is removed from cache
    console.log(await expensiveCalculationMemoized(1, 5)); // using cached result
    console.log(await expensiveCalculationMemoized(1, 2)); // calling expensiveCalculation and caching the result again
    console.log(await expensiveCalculationMemoized(1, 2)); // using cached result
})()

How to use logger with memoize

    const expensiveCalculationMemoized = memoize({
        func: expensiveCalculation,
        cache,
        logger: {
            cacheUsed: () => console.log('CACHE USED'),
            funcCalled: () => console.log('FUNCTION CALLED'),
            error: err => console.error(err)
        }
    });