Memoize in the shell

2025-09-26

So, one day at work, me and my fried David were complaining about how slow requests to AWS can be, using the cli, and what a pain it is to iterate on a slow feedback loop. David remarked, "I wish we could memoize this". That stopped me in my tracks. "Yeah, why don't we?"

A flash later, we had memoize as a shell function.

It doesn't have any testing rig, but if it did, there are a few cases we have considered:

  • The return code, stdout and stderr should all be replicated.

  • The whole of the command is used in the cache key. That is, invokations with different arguments are cached separately.

  • Waiting to read something from either stdout or stderr before reading the other should not block.

  • Cache entries can be cleared individually.

  • Cache can age out with a TTL

  • Removing empty files in cache does not affect results

  • Removing cache completely does not cause a crash

RSS
https://blog.eurenius.eu/posts/feed.xml