1 unstable release
0.1.0 | Apr 28, 2020 |
---|
#1718 in Development tools
16KB
244 lines
goldfish
This is a basic LRU cache backed by an append-only log file. I use it along
with cd
, neovim
, and fzf
, in order to have recently-viewed
directories and recently-edited files available via fuzzy search. It's meant to
be a more transparent alternative to tools like z
, and therefore doesn't
try to predict or rank anything. It also supports multiple caches via the
--cache
flag.
goldfish
implements first-class support for caching directory and file paths, in
the sense that it will a) canonicalize paths and b) check if the path
is a file or directory before storing it in the cache. This means the paths
should be valid from any working directory, so you can always cd
to or edit them.
This behavior can be enabled by passing the --type
(-t
) flag to goldfish put
, e.g.
> goldfish --cache files put --type file src/foo.rs
> goldfish --cache files put --type file src/
> goldfish --cache files get
/home/nwtnni/projects/example/src/foo.rs
Usage
goldfish
is fairly low-level and meant to be called from a shell script or alias.
For example, here's the Bash function I use to change directories:
# Open a directory, list its contents, and cache its path.
#
# If no directory is provided, then use `fzf` to select from the directory cache.
o() {
if [ -z "$1" ]; then
dir=$(goldfish --cache directories get | fzf)
if [ ! -z "$dir" ]; then o "$dir"; fi
else
cd "$1" && ls && goldfish --cache directories put --type dir "$1"
fi
}
More detailed options can be viewed with the --help
flag, i.e. goldfish --help
.
Installation
Currently requires a Rust installation, and is only available from either:
> cargo install goldfish
- Building from source
> git clone https://github.com/nwtnni/goldfish.git
> cargo build --release
> ./target/release/goldfish
Implementation
Log entries are just the data followed by a u16
length footer. This allows
append operations to blindly write to the end of the file. Reading the log is
done backwards, starting at the last two bytes and seeking back to get each
piece of data, which allows us to avoid reading the whole file and efficiently
build the set of most recent entries.
(I'm not entirely sure what performance impact reading a file backwards has on, say, caching or prefetching. This would require some benchmarking.)
Disclaimer
There are several articles stating that goldfish actually have decent memory spans, contrary to the popular myth, but I couldn't find many sources. The closest I found was this radio transcript and this science project.
Dependencies
~5.5MB
~91K SLoC