Skip to content

Instantly share code, notes, and snippets.

View alexanderscott's full-sized avatar

Alex Ehrnschwender alexanderscott

View GitHub Profile
@alexanderscott
alexanderscott / SPUBLISH.lua
Created January 26, 2015 08:50
Redis Lua script to publish a message to all SET member channels
-- @desc: Publish a message to all SET member channels
-- @usage: redis-cli EVAL "$(cat SPUBLISH.lua)" 1 <setKey> <message>
-- @return: number of SET member channels
local function SPUBLISH(key, msg)
local members = redis.call("SMEMBERS", key)
for _,member in ipairs(members) do
redis.call("PUBLISH", member, msg)
end
@alexanderscott
alexanderscott / SETCLAMP.lua
Created January 26, 2015 08:49
Redis Lua script to clamp a value between a min & max
-- @desc: Clamp a value between a min & max
-- @usage: redis-cli EVAL "$(cat SETCLAMP.lua)" 1 <key> <min> <max>
-- @return: clamped value
local function SETCLAMP(key, min, max)
local valStr = redis.call("GET", key)
if not valStr then return end
local val = tonumber(valStr)
if val > max then
@alexanderscott
alexanderscott / MGETSUM.lua
Created January 26, 2015 08:48
Redis Lua script to sum the provided key values
-- @desc: Sum the provided key values
-- @usage: redis-cli EVAL "$(cat MGETSUM.lua)" N <key1> <key2> ... <keyN>
-- @return: sum
local function MGETSUM(keys)
local sum = 0
for _,key in ipairs(keys) do
local val = redis.call('GET', key) or 0
sum = sum + tonumber(val)
@alexanderscott
alexanderscott / MZCARD.lua
Created January 26, 2015 08:48
Redis Lua script to get size of multiple ZSETs
-- @desc: Get size of multiple ZSETs
-- @usage: redis-cli EVAL "$(cat MZCARD.lua)" N <zset1> <zset2> ... <zsetN>
-- @return: list of sizes
local function MZCARD(keys)
local sizes = {}
for _,key in ipairs(keys) do
table.insert(sizes, redis.call("ZCARD", key))
end
return sizes
@alexanderscott
alexanderscott / MEXISTS.lua
Created January 26, 2015 08:47
Redis Lua script to determine which keys exist from a list
-- @desc: Determine which keys exist from a list
-- @usage: redis-cli EVAL "$(cat MEXISTS.lua)" N <key1> <key2> ... <keyN>
-- @return: list of existence in binary representation (ex/ {0, 1, 1, 0})
local function MEXISTS(keys)
local exists = {}
for _,key in ipairs(keys) do
table.insert(exists, redis.call("EXISTS", key))
end
return exists
@alexanderscott
alexanderscott / COPY.lua
Last active March 22, 2019 21:28
Redis Lua script to copy a key without knowing its type
-- @desc: Copy a key without knowing its type
-- @usage: redis-cli EVAL "$(cat COPY.lua)" 2 <key> <destKey>
local function COPY(key, dest)
local keyType = redis.call("TYPE", key)
if keyType == "set" then redis.call("SUNIONSTORE", dest, key)
elseif keyType == "zset" then redis.call("ZUNIONSTORE", dest, 1, key)
elseif keyType == "list" then redis.call("SORT", key, "BY", "NOSORT", "STORE", dest)
elseif keyType == "string" then redis.call("SET", dest, redis.call("GET", key))
@alexanderscott
alexanderscott / gist:c4ad46a7e1b16735df5d
Created November 27, 2014 04:56
Simple example using Punctual - a Redis-backed task scheduler for node.js
"use strict";
var util = require('util'),
Punctual = require('punctual');
/**
* Define a simple TaskRunner for logging to console
*/
function ConsolePrinter(){
@alexanderscott
alexanderscott / git-prune-file.sh
Created September 7, 2014 03:56
remove file from git history, prune & garbage collect, push removal to origin
#!/bin/bash
# Get filename from cmd-line arg
filename=$1
# Fetch all remote branches locally and setup tracking
for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master`; do
git branch --track ${branch##*/} $branch
done
@alexanderscott
alexanderscott / git-local-branch-tracking.sh
Created September 7, 2014 03:52
locally track all git remote branches
#!/bin/bash
for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master`; do
git branch --track ${branch##*/} $branch
done
@alexanderscott
alexanderscott / git-list-large-files.sh
Created September 7, 2014 03:51
list largest files in git history for pruning and garbage collection
#!/bin/bash
#set -x
# Shows you the largest objects in your repo's pack file.
# Written for osx.
#
# @see http://stubbisms.wordpress.com/2009/07/10/git-script-to-show-largest-pack-objects-and-trim-your-waist-line/
# @author Antony Stubbs
# set the internal field spereator to line break, so that we can iterate easily over the verify-pack output