
Product
Socket for Jira Is Now Available
Socket for Jira lets teams turn alerts into Jira tickets with manual creation, automated ticketing rules, and two-way sync.
dyna-cache
Advanced tools
Caches something and provides it immediately.
Preload, refresh features out of the box.
This factory function creates another function that uses the cache behind this.
Suppose we have this interface of articles
interface ITestArticle {
title: string;
body: string;
}
And this api method that loads the articles from the backend
const apiLoadArticles = async (args: {lang: string, group: string}): Promise<ITestArticle[]> => [];
Let's make api method cacheable version of it
const apiLoadArticlesCacheEngine = createDynaCache({
load: apiLoadArticles,
expireAfterMinutes: 10,
});
The apiLoadArticlesCacheEngine is na object, with the load(args: TArgs) method that returns data.
Thanks to typescript, the function has all types obtained from the load method, for the args and for the output.
So we don't have to define anything to the generics of the createDynaCache.
Let's use it
apiLoadArticlesCacheEngine.load({lang: 'en', group: 'fashion'}) // We pass the object as we do with the apiLoadArticles
.then(articles => {
// We have types articles here
articles.forEach(article => {
console.log('Title', article.title);
});
});
createDynaCacheinterface ICreateDynaCacheConfig<TArgs, TData, > {
load: (args: TArgs) => Promise<TData>; // The load operation
expireAfterMinutes?: number; // When the cache expires, undefined for none
preload?: boolean; // Preload the data on start
refreshEveryMinutes?: number; // Refresh the data silently on background
}
To invalidate the cache (to clear the content of the cache) call the invalidate()
apiLoadArticlesCacheEngine.invalidate();
Cache keeps resources for refreshing, etc.
If you are going to free the owner of it, your have to call the .free() also!
Just apiLoadArticlesCacheEngine.free().
The apiLoadArticlesCacheEngine creates internally small caches for each different args.
If you ask the same args, then the cached version will be served.
This is how to create a DynaCache class.
interface IDynaCacheConfig<TData> {
load: () => Promise<TData>; // The load operation
expireAfterMinutes?: number; // When the cache expires, undefined for none
preload?: boolean; // Preload the data on start
refreshEveryMinutes?: number; // Refresh the data silently on background
onLoad?: (data: TData) => void; // Called when the data are loaded for any reason
}
load(): Promise<TData> load from cache or from source if it is not yet cachedloadFresh(): Promise<TData> load fresh data (not from cache)invalidate(): void clear the cache, all further loads will be newfree(): void destroy the cache (cleans up auto refresh cache)get size(): number returns the size of the cache in bytesget loadedAt(): number timestamp when the last load took placeget lastUsedAt(): number timestamp when the cache read last timeget loadCount(): number how many the cache was updatedget lastError(): any last load error// Factory function that create a DynaCache with specific args and api method
// This function just connects that arguments with the api method
const createLoadArticlesCache = (lang: string, group: string) =>
new DynaCache({
expireAfterMinutes: 10,
load: () => apiLoadArticles(lang, group),
});
// Lets create a cache for the epi
const loadArticlesCache = createLoadArticlesCache('en', 'fashion');
// Lets use it
loadArticlesCache.load(); // The return is always up to date and fast
// Don't forget to free() it to avoid memory leaks!
// This is needed when `expireAfterMinutes` or `refreshEveryMinutes`
loadArticlesCache.free();
// Note, now you cannot use it anymore
V3 is totally different approach than V2.
V3 offers plus
For v2 open this page.
FAQs
Cache with controlled memory consumption
We found that dyna-cache demonstrated a not healthy version release cadence and project activity because the last version was released 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.

Product
Socket for Jira lets teams turn alerts into Jira tickets with manual creation, automated ticketing rules, and two-way sync.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.

Security News
NIST will stop enriching most CVEs under a new risk-based model, narrowing the NVD's scope as vulnerability submissions continue to surge.