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

@dharmendrasha/function_cache

v1.0.3

Published

Manages and records the outgoing records from nodejs application.

Downloads

2

Readme

@dharmendrasha/function_cache

The cache function is a TypeScript method decorator designed to cache the result of a method call using the node-cache library. The decorator is applied to class methods, and it caches the results of the method calls in memory, reducing the need to recompute the results for repeated method calls with the same arguments.

The cache decorator accepts an optional ttl (time-to-live) parameter, which determines how long the cached results should be retained in memory before expiring. If no ttl is provided, the default value of 10 seconds is used.

The decorator works in the following way:

  1. When a method decorated with @cache is called, the decorator first generates a cache key based on the class name, method name, and method arguments. It then checks if the cache contains a result associated with that key.
  2. If the cache contains a result for the given key, the decorator returns the cached result immediately, and the original method is not called.
  3. If the cache does not contain a result for the given key, the original method is called with the provided arguments, and its result is stored in the cache with the corresponding cache key.
  4. The cached result is associated with the specific combination of method arguments, so different arguments will produce different cache entries, ensuring correct caching behavior.

The decorator handles various scenarios, such as caching methods that throw errors and excluding methods that return undefined or null from being cached. Additionally, it takes argument types into account, so different types of arguments will not produce cache hits.

This decorator simplifies the caching process for methods, reducing computational overhead and improving the performance of the decorated methods, especially for methods with expensive computations or data retrieval operations.

  1. GITHUB link

  2. NPM link

Installation

Install this package with multiple package manager like pnpm | yarn | yarn take a look these commands

# npm
npm i @dharmendrasha/function_cache

# yarn
yarn install @dharmendrasha/function_cache

# pnpm
pnpm add @dharmendrasha/function_cache

Usage

Typescript

eg.

//OutGoingRequestHandler.ts
import "reflect-metadata";
import cache from '@dharmendrasha/function_cache';


class Person {
  @cache(10) // TTL parameter passed directly to @cache
  fullName(firstName: string, LastName: string) {
    
    return {[firstName]: LastName};
  }
}

const person = new Person();

// first call to the method - result stored in cache
const result = person.fullName('John', 'Doe');
console.log(result) // {"John": "Doe"}

// call with different parameters, original method is called
const result2 = person.fullName('Filipe', 'Silve');
console.log(result2) // {"Filipe": "Silve"}


// result3 is returned from cache
const result3 = person.fullName('John', 'Doe');
console.log(result3)// {"John": "Doe"}

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT