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

@methodus/throng

v1.0.2

Published

Throng is a Caching & Throttle TypeScript decorators.

Downloads

20

Readme

Quality Gate Status Coverage Bugs

Total alerts Language grade: JavaScript

Throng

Throng decorators enable caching & throttling of class methods in your app.

meaning: a large, densely packed crowd of people or animals. "he pushed his way through the throng"

An unintrusive approach for cache and throttle, that can be applied any where in a typescript application.

Install

npm i @methodus/throng

Usage

import {Cache, Throttle} from '@methodus/throng';

export class TestClass{

    @Cache(120, 2) // 120 is the cache TTL, 2 is the number of concurrent executions for the expire rerun
    public async cacheMethod(key1:string, key2:string: ){
        /// function code
        return;
    }

    @Throttle(3) // 3 in the number of concurrent operations
    public async throttledMethod(){
        /// function code
        return;
    }

    @Cache(120) // 120 is the cache TTL
    @Throttle(3) // 3 in the number of concurrent operations
    public async combinedMethod(){
        /// function code
        return;
    }

}
@Cache( ttlTime: number, 
       reloadThrottleLimit: number,
       keyLength?: number | Function, 
       setCacheFunction?: Function,
       filterCacheFunction?: Function )

ttlTime

Time in seconds until records expire.

reloadThrottleLimit

A number value or a function returning it for the amount of concurrent executions when refreshing the cache record.

keyLength

A number value or a function returning a string key to be used as the cache item key. If a number is returned it will be used as the amount of arguments to take from the function args for the creation of the key hash.

setCacheFunction

the return value of the function will be stored in the cache, if false is returned nothing will be stored in cache.

@Cache(120, 2, ,2 , (data)=>{ return somenewData }) 
    public async cacheMethod(key1:string, key2:string: ){
        /// function code
        return;
    }

filterCacheFunction

This function will be called before the cache item check, if it return false, the entire cache operation will be skipped (no-cache)

@Cache(120, 2, 2 ,null,  (args)=>{ return args[2] // arg2 is the 3 argument of the decorated function }) 
    public async cacheMethod(key1:string, key2:string, useCache: boolean ){
        /// function code
        return;
    }

@Throttle( limit: number)

limit

Time limit for concurrent execution of the method.

Global throttle limit

By default Throttle uses a global limit object, if you wish to have seperated queues disable the default by passing: THROTTLE_GLOBALLY with the value false

Debug

set "DEBUG" env variable to:

methodus:throng:*
methodus:throng:cache
methodus:throng:throttle

Env Options

CACHE_CHECK_PERIOD:number - the interval for cache expiry check.

CACHE_DELETE_ON_EXPIRE=true: boolean-string - whether to delete the objects as they expire.

CACHE_RELOAD_ON_EXPIRE=false: boolean-string - whether to execute the original function at the end of TTL.

CACHE_USE_CLONES=true: boolean-string - whether to use the original objects in the cache collection or make copies.

THROTTLE_GLOBALLY=true: boolean-string - use a global limit for all decorated methods (throttle);

boolean-string means 'true' | 'false'

Disable

pass THRONG_OFF to completly disable