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 / 00-rld-start.lua
Last active April 19, 2017 20:28
Snippets accompanying Redis Lua Debugger blog post
rld.start()
@itamarhaber
itamarhaber / statsC50-T-1-pipe-75-D-100-Client1-Test3.txt
Created August 22, 2014 03:28
Unbenchmarking Redis on an EC2 instance
1 Threads
50 Connections per thread
1000000 Requests per thread
Type Ops/sec Hits/sec Misses/sec Latency KB/sec
------------------------------------------------------------------------
Sets 198412.70 --- --- 9.04900 28440.00
Gets 198412.70 198412.70 0.00 9.01200 27471.00
Totals 396825.40 198412.70 0.00 9.03000 22623.00
@itamarhaber
itamarhaber / 00 Turbo Boost WordPress with a Secure Memcached Plugin
Last active August 29, 2015 14:04
Snippets accompanying Turbo Boost WordPress with a Secure Memcached Plugin blog post (http://redislabs.com/blog/turbo-boost-wordpress-with-a-secure-memcached-plugin)
Follow these steps to deploy WordPress, Memcached Cloud and the Memcached Cloud Plugin for Wordpress on Heroku.
@itamarhaber
itamarhaber / index.html
Last active March 29, 2019 02:26
Red is Beautiful: A Visualization of Redis Commands
<!DOCTYPE html>
<!--
Red is Beautiful: A Visualization of Redis Commands
By Itamar Haber, Redis Labs
Adopted from Mike Bostock's Zoomable Pack Layout example: http://mbostock.github.io/d3/talk/20111116/pack-hierarchy.html
!-->
<html>
<head>
<style>
@itamarhaber
itamarhaber / redis_suffix_index.lua
Created May 23, 2014 15:41
Redis suffix index function in Lua. Call with key name of the search terms set and argument to index
redis.call("zadd", KEYS[1], 1, ARGV[1]:reverse())
@itamarhaber
itamarhaber / redis_suffix_search.lua
Last active April 19, 2017 21:04
Redis suffix search function in Lua. Call with key name of search terms set and the argument to search
local res = redis.call("zrangebylex", KEYS[1], "\[" .. ARGV[1]:reverse(), "\[" .. ARGV[1]:reverse() .. "\\xff")
for k, v in pairs(res) do
res[k] = v:reverse()
end
return res
@itamarhaber
itamarhaber / scan_del.sh
Created April 20, 2014 22:27
A bash script that deletes Redis keys by pattern using SCAN
#!/bin/bash
if [ $# -ne 3 ]
then
echo "Delete keys from Redis matching a pattern using SCAN & DEL"
echo "Usage: $0 <host> <port> <pattern>"
exit 1
fi
cursor=-1
@itamarhaber
itamarhaber / hitman.py
Created April 19, 2014 16:45
Kill idle Redis connections
import redis
import re
idle_max = 300
r = redis.Redis(host="localhost", port=6379, password=None)
l = r.execute_command("client", "list")
pattern = r"addr=(.*?) .*? idle=(\d*)"
regex = re.compile(pattern)
@itamarhaber
itamarhaber / hitman.lua
Created April 19, 2014 16:12
Kill idle Redis connections
local l = redis.call('client', 'list')
local k = 0
for r in l:gmatch('[^\n]+') do
for c,i in r:gmatch('addr=(.+:%d+).*idle=(%d+).*') do
if tonumber(i) > tonumber(ARGV[1]) then
redis.call('client', 'kill', c)
k = k + 1
end
end