Skip to content

Instantly share code, notes, and snippets.

@ptbrowne
Last active July 23, 2019 08:28
Show Gist options
  • Save ptbrowne/f697899b2212757f4b857d794e48ea30 to your computer and use it in GitHub Desktop.
Save ptbrowne/f697899b2212757f4b857d794e48ea30 to your computer and use it in GitHub Desktop.

Revisions

  1. ptbrowne revised this gist Jul 23, 2019. No changes.
  2. ptbrowne revised this gist Jul 23, 2019. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -9,12 +9,12 @@ immediately.
    ## Installation

    ```
    wget <THIS_GIST> -O cache.js
    ln -s $(pwd)/cache.js ~/bin/cache
    wget https://gist.github.com/ptbrowne/f697899b2212757f4b857d794e48ea30/raw/9b6e96506e2c3b0c64bceea70302f5597c452272/cache.js -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.
    is in your `PATH`.

    ## Usage

  3. ptbrowne revised this gist Jul 23, 2019. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -27,9 +27,8 @@ is in your PATH.

    ## How does it work

    `cache` makes a key out of all the arguments, hashes it to make a `<key>`, and looks for /tmp/`<key>`. If the
    file exists, it outputs its content (max length 10Mo).
    `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 the key. If it changes, the `<key>` changes
    and the result of the command is recomputed.
    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.
  4. ptbrowne revised this gist Jul 23, 2019. No changes.
  5. ptbrowne revised this gist Jul 23, 2019. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    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 always wait those seconds when you know the result has not changed.
    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.
  6. ptbrowne created this gist Jul 23, 2019.
    34 changes: 34 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    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 always wait those seconds 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.

    ## Installation

    ```
    wget <THIS_GIST> -O cache.js
    ln -s $(pwd)/cache.js ~/bin/cache
    ```

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

    ## Usage

    `cache` "decorates" the command you give it.

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

    ## How does it work

    `cache` makes a key out of all the arguments, 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 the key. If it changes, the `<key>` changes
    and the result of the command is recomputed.
    31 changes: 31 additions & 0 deletions cache.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    #!/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 = args.map((argName, argIndex) => {
    return {
    argName,
    argIndex,
    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)) {
    console.log(fs.readFileSync(filename).toString())
    } else {
    child_process.exec(args.join(' '), { maxBuffer: 1024*1024*1024 }, (error, stdout) => {
    if (error) {
    console.error(error)
    process.exit(1)
    } else {
    fs.writeFileSync(filename, stdout)
    console.log(stdout)
    }
    })
    }