
Cacheble Utils

@cacheable/utils is a collecton of utility functions, helpers, and types for cacheable and other caching libraries. It provides a robust set of features to enhance caching capabilities, including:
- Data Types for Caching Items
- Hash Functions for Key Generation
- Coalesce Async for Handling Multiple Promises
- Stats Helpers for Caching Statistics
- Sleep / Delay for Testing and Timing
- Time to Live (TTL) Helpers
Table of Contents
Getting Started
cacheable is primarily used as an extension to your caching engine with a robust storage backend Keyv, Memoization (Wrap), Hooks, Events, and Statistics.
npm install @cacheable/utils --save
Cacheable Types
The @cacheable/utils package provides various types that are used throughout the caching library. These types help in defining the structure of cached items, ensuring type safety and consistency across your caching operations.
export type CacheableItem = {
key: string;
value: any;
ttl?: number | string;
};
export type CacheableStoreItem = {
key: string;
value: any;
expires?: number;
};
Coalesce Async
The coalesceAsync function is a utility that allows you to handle multiple asynchronous operations efficiently. It was designed by Douglas Cayers https://github.com/douglascayers/promise-coalesce. It helps in coalescing multiple promises into a single promise, ensuring that only one operation is executed at a time for the same key.
import { coalesceAsync } from '@cacheable/utils';
const fetchData = async (key: string) => {
return new Promise((resolve) => setTimeout(() => resolve(`Data for ${key}`), 1000));
};
const result = await Promise.all([
coalesceAsync('my-key', fetchData),
coalesceAsync('my-key', fetchData),
coalesceAsync('my-key', fetchData),
]);
console.log(result);
Hash Functions
The @cacheable/utils package provides hash functions that can be used to generate unique keys for caching operations. These functions are useful for creating consistent and unique identifiers for cached items.
import { hash } from '@cacheable/utils';
const key = hash('my-cache-key');
console.log(key);
If you want to get a number hash you can use the hashToNumber function:
import { hash, hashToNumber } from '@cacheable/utils';
const min = 0;
const max = 10;
const result = hashToNumber({foo: 'bar'}, min, max, HashAlgorithm.DJB2);
console.log(result);
Shorthand Time Helpers
The @cacheable/utils package provides a shorthand function to convert human-readable time strings into milliseconds. This is useful for setting time-to-live (TTL) values in caching operations.
You can also use the shorthandToMilliseconds function:
import { shorthandToMilliseconds } from '@cacheable/utils';
const milliseconds = shorthandToMilliseconds('1h');
console.log(milliseconds);
You can also use the shorthandToTime function to get the current date plus the shorthand time:
import { shorthandToTime } from '@cacheable/utils';
const currentDate = new Date();
const timeInMs = shorthandToTime('1h', currentDate);
console.log(timeInMs);
Sleep Helper
The sleep function is a utility that allows you to pause execution for a specified duration. This can be useful in testing scenarios or when you need to introduce delays in your code.
import { sleep } from '@cacheable/utils';
await sleep(1000);
console.log('Execution resumed after 1 second');
Stats Helpers
The @cacheable/utils package provides statistics helpers that can be used to track and analyze caching operations. These helpers can be used to gather metrics such as hit rates, miss rates, and other performance-related statistics.
import { stats } from '@cacheable/utils';
const cacheStats = stats();
cacheStats.incrementHits();
console.log(cacheStats.hits);
Time to Live (TTL) Helpers
The @cacheable/utils package provides helpers for managing time-to-live (TTL) values for cached items.
You can use the calculateTtlFromExpiration function to calculate the TTL based on an expiration date:
import { calculateTtlFromExpiration } from '@cacheable/utils';
const expirationDate = new Date(Date.now() + 1000 * 60 * 5);
const ttl = calculateTtlFromExpiration(Date.now(), expirationDate);
console.log(ttl);
You can also use getTtlFromExpires to get the TTL from an expiration date:
import { getTtlFromExpires } from '@cacheable/utils';
const expirationDate = new Date(Date.now() + 1000 * 60 * 5);
const ttl = getTtlFromExpires(expirationDate);
console.log(ttl);
You can use getCascadingTtl to get the TTL for cascading cache operations:
import { getCascadingTtl } from '@cacheable/utils';
const cacheableTtl = 1000 * 60 * 5;
const primaryTtl = 1000 * 60 * 2;
const secondaryTtl = 1000 * 60;
const ttl = getCascadingTtl(cacheableTtl, primaryTtl, secondaryTtl);
How to Contribute
You can contribute by forking the repo and submitting a pull request. Please make sure to add tests and update the documentation. To learn more about how to contribute go to our main README https://github.com/jaredwray/cacheable. This will talk about how to Open a Pull Request, Ask a Question, or Post an Issue.
License and Copyright
MIT © Jared Wray