ZL
About Articles Contact
Published on Sep 26, 2023
Filed under:
#node,
#caching,
#splendidlabz

Easy caching on the local file system with Node

Today I want to introduce you a utility that will make caching in the local file system extremely easy.

This utility is called fileCache and it’s built into Splendid UI.

Terminal window
npm install splendidlabz

To use the utility, import fileCache from Splendid UI.

Give it a directory and a file name.

  • The directory determines where to store the cached file.
  • The file name determines the name of the file.
import { fileCache } from 'splendidlabz/node'
const cache = await fileCache({
dirname: '.cache',
file: `${id}.json`,
})

When you want to save values into the cache, just use the save method. Values will automatically be converted into JSON.

const objectToSave = { some: 'value' }
await cache.save(objectToSave)

When you want to load the cache, use the load method. This method returns a promise.

cache.load()

Invalidating the cache

fileCache returns the file’s modifiedAt timestamp. If you’re familiar with Node, this is the mstimeMs value you get from fs.stat.

You can use this modifiedAt timestamp to check whether you want to invalidate the cache.

function getValues () {
const cache = await fileCache(/* ... */)
const Date = new Date()
// Load the cache
if (cache.modifiedAt > timestamp) {
return cache.load()
}
// Otherwise, create the cache
const obj = { some: 'value'}
await cache.save(obj)
return obj
}

Invalidating the cache when other files change

It’s more common for cache files to be created with the content from other files. In this case, if these source files change, you will want to invalidate the cache.

It’s easy to do this with the getLastModifiedTime utility from Splendid UI.

getLastModifiedTime returns the last modified time from a list of files. You can use this to decide whether to invalidate the cache.

It takes in a glob value so you can specify all the files you want to check. This process is extremely quick so you don’t have to worry about checking lots of files at once.

import { getLastModifiedTime } from 'splendidlabz/node'
// Gets the latest modified timestamp of all the files in src/content
const lastModified = await getLatestModifiedTime('src/content/**/*')

If the latest modified timestamp exceeds that of the cache, you can invalidate the cache and generate a new one.

The process looks like this:

import { fileCache, getLastModifiedTime } from 'splendidlabz/node'
function getContent () {
const cache = await fileCache({ /* ... */ })
const lastModified = await getLatestModifiedTime(`src/content/`)
// Load the cache
if (cache.modifiedAt > lastModified) {
return cache.load()
}
// Otherwise, create the cache
const obj = { some: 'value'}
await cache.save(obj)
return obj
}

That’s it!

Further Reading

Check out the documentation for fileCache and getLatestModifiedTime for more information.

  • fileCache
  • getLatestModifiedTime

Lastly, if you enjoyed this, you may also enjoy the utilities I’ve put together in Splendid UI.

Previous The main challenge of building an Astro-first component library Next 3 Techniques to learn JavaScript effectively

Join My Newsletter

I share what I’m learning on this newsletter: code, building businesses, and living well.

Sometimes I write about technical deep-dives, product updates, musings on how to live, and sometimes my struggles and how I’m breaking through.

Regardless of the type of content, I do my best to send you an update every week.

If you’re into making things and growing as a person, you’ll probably feel at home here.

“

Zell is one of those rare people who commands tremendous knowledge and experience but remains humble and helpful. They want you to know what they know, not just be impressed by it.

In other words, Zell is a natural teacher. You’re lucky to have him because he feels lucky to be able to help you in your journey.

Heydon Pickering
Heydon Pickering — Web & Accessibility Extraordinaire
The Footer

General

Home About Contact Testimonials Tools I Use

Projects

Magical Dev School Splendid Labz

Socials

Youtube Instagram Tiktok Github Bluesky X

Follow Along

Email RSS
© 2013 - 2025 Zell Liew / All rights reserved / Terms