Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

faster-query

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

faster-query

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

  • 2.0.4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5
increased by400%
Maintainers
1
Weekly downloads
 
Created
Source

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');

Clearing Intervals and Timers

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

Keywords

FAQs

Package last updated on 16 Feb 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc