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

faster-query

v2.0.6

Published

Faster query for any slow function. Get any data from cache and update in cache AFTER data is received

Downloads

8

Readme

FasterQuery (ts)

FasterQuery is a simple yet powerful utility for caching the results of asynchronous functions in Node.js applications. It provides a convenient way to store and retrieve the output of functions, reducing the need for repetitive computations and improving overall application performance.

Features

  • Flexible Caching Options: FasterQuery offers various caching options, including time-to-live (TTL) expiration and automatic updates by interval.
  • File System Storage: Results are stored in the file system, ensuring persistence across application restarts.
  • Debugging Support: Integrated logging functionality allows developers to track cache hits, misses, and updates for debugging purposes.
  • Easy Integration: With a straightforward API, FasterQuery can be seamlessly integrated into existing projects with minimal configuration.

The Key of cache

The key is generated by hashing the string representation of the function and its arguments using the MD5 hashing algorithm. This ensures that the same set of function and arguments will always produce the same key, allowing for consistent caching and retrieval of results.

Installation

You can install FasterQuery via npm:

npm install faster-query

Usage

Function: get

Purpose:

The get function returns a memoized version of an asynchronous function with caching options.

Parameters:

  • fn (AsyncFunction<T>): The asynchronous function to be memoized.
  • options (CacheOptions): The caching options to customize the behavior of the cache.

Returns:

  • AsyncFunction<T>: A memoized version of the input asynchronous function.

Description:

The get function accepts an asynchronous function (fn) and caching options (options). It returns a new asynchronous function that wraps the original function (fn) with caching logic based on the provided options. This memoized function automatically caches the results of the original function and retrieves them from the cache when the same set of arguments is provided, thus improving performance by avoiding redundant computations.

Example Usage:

const cachedAsyncFunction = fasterQueryInstance.get(asyncFunctionToMemoize, {
    ttl: 60 * 60, // hour
    returnCachedIfExpiredAndUpdate: true
});

CacheOptions

ttl (Time To Live) (defaults to 60 seconds): Determines the lifespan of cached data in seconds. After this time elapses, the data is considered expired and may be updated or deleted depending on other parameters.

and one of the

  • returnCachedIfExpiredAndUpdate (defaults to false): If set to true, and the cached data has expired (more time has passed than ttl), the expired value is immediately returned from the cache and then updated by invoking the function and updating the cache.

  • autoUpdateDataByInterval (defaults to false): If set to true, cached data will be automatically updated at regular intervals === (TTL - 2 sec). This ensures that the cached data remains fresh by periodically invoking the function and updating the cache.

  • deleteAfterExpiration (defaults to false): If set to true, the cached data will be deleted after it expires. This is useful for scenarios where expired data should not be retained in the cache.

import FasterQuery from 'faster-query';
const cached = new FasterQuery('/path/to/cache');

export const cachedDataBySlug = await cached.get(async (slug: string) => dbQuery(slug), {
    ttl: 60 * 60, // hour
    autoUpdateDataByInterval: true // alwais fast answer and data not older then 1 hour
})
//---
const result = await cachedDataBySlug('slug/to/get/data');

or

import FasterQuery from 'faster-query';

const cached = new FasterQuery('/path/to/cache');

const getDataBySlug = async (slug: string) => {
    return dbQuery(slug);
};

export const cachedDataBySlug = await cached.get(getDataBySlug, {
    ttl: 60, // seconds
    autoUpdateDataByInterval: true,
});
//---
const result = await cachedDataBySlug('slug/to/get/data');

Logging

import FasterQuery from 'faster-query';

FasterQuery.isLogging = true

Logging only in development

Clearing Intervals and Timers

On process.on('exit', ...); script clearing all Intervals and Timers