Skip to content

Instantly share code, notes, and snippets.

@itamarhaber
itamarhaber / 00_copy_key.lua
Last active December 14, 2023 14:14
The fastest, type-agnostic way to copy a Redis key, as discussed in https://redislabs.com/blog/the-7th-principle-of-redis-we-optimize-for-joy
-- @desc: The fastest, type-agnostic way to copy a Redis key
-- @usage: redis-cli --eval copy_key.lua <source> <dest> , [NX]
local s = KEYS[1]
local d = KEYS[2]
if redis.call("EXISTS", d) == 1 then
if type(ARGV[1]) == "string" and ARGV[1]:upper() == "NX" then
return nil
else
@itamarhaber
itamarhaber / Makefile
Last active August 8, 2023 15:47
Module development environment walkthrough
SHOBJ_CFLAGS ?= -fno-common -g -ggdb
SHOBJ_LDFLAGS ?= -shared -Bsymbolic
CFLAGS = -Wall -g -fPIC -lc -lm -Og -std=gnu99
CC=gcc
all: example.so
example.so: example.o
$(LD) -o $@ example.o $(SHOBJ_LDFLAGS) $(LIBS) -lc
@itamarhaber
itamarhaber / hashslot.lua
Created November 26, 2022 16:47
Redis key to hashslot in Lua
--/*
-- Examples:
-- $ redis-cli --eval hashslot.lua , key
-- (integer) 12539
-- $ redis-cli --eval hashslot.lua , key2
-- (integer) 4998
-- $ redis-cli --eval hashslot.lua , key3
-- (integer) 935
-- $ redis-cli --eval hashslot.lua , "id:{key}"
-- (integer) 12539
@itamarhaber
itamarhaber / some_thoughts.md
Created February 23, 2018 19:18
Some thoughts about "Building a sliding window rate limiter with Redis"

Reading Building a sliding window rate limiter with Redis, and w/o addressing the actual logic (which may or may not work).

Optimize by:

  1. Lua seems a much better choice: idiompotent, portable, server-side, less bandwidth, atomic...
  2. The call to ZRANGEBYSCORE seems to be unused, should be commented out if so
  3. Looking at the use of ZRANGE, it appears that ZCARD it what's actually needed

The (untested) Lua snippet:

@itamarhaber
itamarhaber / orginal-redis-server-logo.txt
Last active October 30, 2021 10:28
Redis ASCII logos
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 999.999.999 (3f9e2322/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 16142
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
@itamarhaber
itamarhaber / setbitrange.lua
Last active September 30, 2021 17:03
A somewhat optimized scripted approach to complementing Redis with a `SETBITRANGE` command
--[[
Sets a bitmap range
Bitmaps are stored as Strings in Redis. A range spans one or more bytes,
so we can call `SETRANGE` when entire bytes need to be set instead of flipping
individual bits. Also, to avoid multiple internal memory allocations in
Redis, we traverse in reverse.
Expected input:
KEYS[1] - bitfield key
#!/bin/bash
# Why? Education & zero redis-cli dependecy
# (it is a shame, however, that this functionality isn't a part of redis-cli... PR on the way)
HOST=127.0.0.1
PORT=6379
if [ $# -lt "2" ]
then
echo "Call a Redis command with its last argument being file contents"
echo "Usage: $0 <Redis command> <keys and arguments for command> <payload>"
@itamarhaber
itamarhaber / tcl-dump.lua
Created June 27, 2021 14:45
Dump a Redis key in TCL binary string encoding
-- DUMPs a Redis key in TCL-compatible binary string encoding
local b = redis.call("DUMP",KEYS[1])
local s = ""
for i = 1, string.len(b), 1 do
s = s .. string.format("\\x%02X",string.byte(b,i))
end
return s
@itamarhaber
itamarhaber / luainsider.md
Last active April 10, 2021 19:09
A micro OSS project, blog post or an addition to http://redis.io/topics|commands/eval|evalsha?

Luainsider - manage your Redis Lua scripts like a pro

A methodology, approach and apparatus for semi-persisting and aliasing Redis Lua scripts.

FAIR WARNING: this is WIP (i.e. could blow in your face)

Description

@itamarhaber
itamarhaber / part1.md
Last active March 1, 2021 15:01
Redis 6.0 & 6.2