Skip to content

Instantly share code, notes, and snippets.

@maxpert
Created December 7, 2022 03:33
Show Gist options
  • Select an option

  • Save maxpert/1de58856ed68ed54a8c1eb4f2b79fe67 to your computer and use it in GitHub Desktop.

Select an option

Save maxpert/1de58856ed68ed54a8c1eb4f2b79fe67 to your computer and use it in GitHub Desktop.

Revisions

  1. maxpert created this gist Dec 7, 2022.
    23 changes: 23 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    Set of Deno scripts with some powerful toolbelt scripts for Redis

    MIT License

    Copyright (c) 2022 Zohaib Sibte Hassan

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all
    copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    SOFTWARE.
    60 changes: 60 additions & 0 deletions redis-pattern-cmd.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    import { Command } from "https://deno.land/x/[email protected]/command/mod.ts";
    import { connect } from "https://deno.land/x/[email protected]/mod.ts";
    import { TerminalSpinner } from "https://deno.land/x/[email protected]/mod.ts";

    await new Command()
    .name("redis-pattern-cmd")
    .description("A simple Redis pattern command executor")
    .version("v1.0.0")
    .option("--redis-port <port:number>", "Port for Redis server.", {
    default: 6379,
    })
    .option("--redis-host <hostname:string>", "The host name for the Redis server.", {
    default: "localhost",
    })
    .option("--page-count <page-count:number>", "Number of items per page when doing scan", {
    default: 1000,
    })
    .option("--command-params <extra-params:string[]>", "Extra space separated parameters to pass for redis commands", {
    default: [],
    separator: " ",
    })
    .arguments("[command:string] [pattern:string] [params...]")
    .example("Delete everything matching a pattern", "DEL foo:*")
    .example("Set TTL of all keys matching pattern with no expiry", `--command-params "60 NX" EXPIRE foo:*`)
    .action(async ({ redisHost, redisPort, pageCount, commandParams: params}, cmd = "TOUCH", pattern = "*") => {
    const redis = await connect({
    hostname: redisHost,
    port: redisPort
    });

    const paramsJoined = params.join(' ');
    const spinner = new TerminalSpinner(`Processing...`);
    let page = 0;

    main_loop:
    do {
    const [nextPage, scannedKeys] = await redis.scan(page, {pattern, count: pageCount});

    for(const k of scannedKeys) {

    spinner.set(`${cmd} ${k.slice(0, 32)} ${paramsJoined} `);
    try {
    await redis.sendCommand(cmd, k, ...params);
    spinner.renderNextFrame();
    } catch (e) {
    spinner.fail(`Error: ${e}`);
    break main_loop;
    }
    }

    page = parseInt(nextPage);
    if (page === 0) {
    spinner.succeed("Completed!");
    }

    } while (page !== 0);
    })
    .parse(Deno.args);