r/commandline • u/diamond414 • Apr 12 '23
New release of bkt, a subprocess caching utility
Hi all, I recently cut a new release of bkt with some additional functionality. Notably, it's now possible to include a file's last-modified time in the cache key, thereby invalidating the cache if the file changes.
Wait, what is bkt?
bkt is a subprocess caching utility you can use to persist a command's output so that subsequent invocations are fast. As an example, I use bkt heavily in my shell prompt to speed up the information it displays.
Another way I use bkt often is to simplify and speed up iterating on command pipelines that are slow to run. For example, if you're using jq to play around with a JSON response you might do something like this:
$ curl http://some.api/data/aaa | jq '.foo'
$ curl http://some.api/data/aaa | jq '.foo.bar'
$ curl http://some.api/data/bbb | jq '.foo.bar.baz'Which is obviously wasteful and slow. You could write the output to a file and then pipe that to jq, but you often end up juggling multiple response files and it get's tedious quickly.
Instead, using bkt ensures each request is only sent once and all subsequent calls return locally cached results:
$ bkt --ttl=1d -- curl http://some.api/data/aaa | jq '.foo'
$ bkt --ttl=1d -- curl http://some.api/data/aaa | jq '.foo.bar'
$ bkt --ttl=1d -- curl http://some.api/data/bbb | jq '.foo.bar.baz'If you haven't used it before give it a spin! If you find it useful please share how you're using bkt so others can benefit :)
3
u/JonathanMatthews_com Apr 12 '23
Interesting - so it’s a filesystem-based memoisation CLI tool? I’ve not seen a generic implementation of that before!
A couple of questions, whilst you’re here:
2
u/[deleted] Apr 13 '23
Funny, I wrote this in pure bash only recently, saving the output in an associative array, where the keys are xxh3 hashed string from the cmdline invocation ($0 $*). I was just about to add the feature of TTL to it and then I discovered your reddit post about bkt.
Oh I got TONS of ideas how to use it.
Primarily I started my version because I was sick and tired of how slow "pip show <pipy-package>" was in a particular script I'm using it, but really, you can use it with anything that you know has a reasonable ttl or when you can counter-check it from the filesystem (e.g. when was the apt-db last changed? if not to recently, then reuse dpk -l and such super-quickly).
Good project!
2
u/[deleted] Apr 14 '23
damn this actually is pretty cool. thank you!