Skip to content

Instantly share code, notes, and snippets.

@ptbrowne ptbrowne/
Last active Jul 23, 2019

What would you like to do?
Simple command cache

If you deal with commands that can take several seconds but always return the same result and you need to transform the result with pipe chain (jq / grep / awk), it can be annoying to wait for those seconds while you incrementally build the command, when you know the result has not changed.

cache.js can help you by caching the result of the command to /tmp and returning the result immediately.


wget -O ~/bin/cache
chmod +x ~/bin/cache

I used ~/bin/ because this directory is already in my PATH but you can use any directory that is in your PATH.


cache "decorates" the command you give it.

- ./long-command input-file.json --my-arg 
+ cache ./long-command input-file.json --my-arg 

How does it work

cache makes an array out of all the arguments (name + index), JSON stringifies it, hashes it to make a <key>, and looks for /tmp/<key>. If the file exists, it outputs its content (max length 10Mo).

Additionally, an extra step is taken to handle file arguments. For each argument, we check a file with the same name exists and if it exists, the modification time is added to information of the argument. If the file changes, the <key> changes and the result of the command is recomputed.

#!/usr/bin/env node
const fs = require('fs')
const child_process = require('child_process')
const crypto = require('crypto')
const args = process.argv.slice(2)
const withCacheInfo =, argIndex) => {
return {
mtime: fs.existsSync(argName) ? fs.statSync(argName).mtime : undefined
const cacheKey = JSON.stringify(withCacheInfo, null, 2)
const hash = crypto.createHash('md5').update(cacheKey).digest("hex")
const filename =`/tmp/${hash}-cache`
if (fs.existsSync(filename)) {
} else {
child_process.exec(args.join(' '), { maxBuffer: 1024*1024*1024 }, (error, stdout) => {
if (error) {
} else {
fs.writeFileSync(filename, stdout)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.