Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
faster-query
Advanced tools
Faster query for any slow function. Get any data from cache and update in cache AFTER data is received
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.
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.
You can install FasterQuery via npm:
npm install faster-query
get
The get
function returns a memoized version of an asynchronous function with caching options.
fn
(AsyncFunction<T>
): The asynchronous function to be memoized.options
(CacheOptions
): The caching options to customize the behavior of the cache.AsyncFunction<T>
: A memoized version of the input asynchronous function.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.
const cachedAsyncFunction = fasterQueryInstance.get(asyncFunctionToMemoize, {
ttl: 60 * 60, // hour
returnCachedIfExpiredAndUpdate: true
});
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.
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');
import FasterQuery from 'faster-query';
FasterQuery.isLogging = true
Logging only in development
On process.on('exit', ...);
script clearing all Intervals and Timers
FAQs
Faster query for any slow function. Get any data from cache and update in cache AFTER data is received
The npm package faster-query receives a total of 3 weekly downloads. As such, faster-query popularity was classified as not popular.
We found that faster-query demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.