diff --git a/demos/demo-redis-quota/README.md b/demos/demo-redis-quota/README.md new file mode 100644 index 0000000..21afe89 --- /dev/null +++ b/demos/demo-redis-quota/README.md @@ -0,0 +1,17 @@ +First, setup redis: bash setup.sh to install redis in ubuntu for thest + +To test, fill a prefix with some random keys (1k each) +bash fill.sh + +Then you can check providing the prefixes to check (as I assume we know them )"" + +bash check.sh ... + +Example + +``` +bash fill.sh mike 100 +bash check.sh mike franz +bash fill.sh franz 200 +bash check.sh mike franz +``` \ No newline at end of file diff --git a/demos/demo-redis-quota/check.sh b/demos/demo-redis-quota/check.sh new file mode 100644 index 0000000..8cdccc4 --- /dev/null +++ b/demos/demo-redis-quota/check.sh @@ -0,0 +1,5 @@ +#!/bin/bash +SCRIPT=$(redis-cli SCRIPT LOAD "$(cat size_by_prefix.lua)") +for i in "$@" +do redis-cli EVALSHA $SCRIPT 0 "$i:" +done \ No newline at end of file diff --git a/demos/demo-redis-quota/fill.sh b/demos/demo-redis-quota/fill.sh new file mode 100644 index 0000000..3c45cf6 --- /dev/null +++ b/demos/demo-redis-quota/fill.sh @@ -0,0 +1,9 @@ +#!/bin/bash +PREFIX=${1?:prefix} +N=${2?:count} +X=$(printf "x%.0s" {1..1024}) +for i in $(seq 1 $N) +do K=$RANDOM + echo $i $K + redis-cli set $PREFIX:$K "$X" +done diff --git a/demos/demo-redis-quota/setup.sh b/demos/demo-redis-quota/setup.sh new file mode 100644 index 0000000..d474de8 --- /dev/null +++ b/demos/demo-redis-quota/setup.sh @@ -0,0 +1,4 @@ +sudo apt-get install redis-server redis-tools +sudo systemctl start redis-server +sudo systemctl status redis-server + diff --git a/demos/demo-redis-quota/size_by_prefix.lua b/demos/demo-redis-quota/size_by_prefix.lua new file mode 100644 index 0000000..6cefa04 --- /dev/null +++ b/demos/demo-redis-quota/size_by_prefix.lua @@ -0,0 +1,21 @@ +-- Define the key prefix to search for +local prefix = ARGV[1] +local cursor = 0 +local total_memory = 0 + +repeat + -- Use SCAN to get keys that match the prefix + local result = redis.call('SCAN', cursor, 'MATCH', prefix .. '*') + cursor = tonumber(result[1]) + local keys = result[2] + + for _, key in ipairs(keys) do + -- Get memory usage of each key and add it to the total + local memory_usage = redis.call('MEMORY', 'USAGE', key) + if memory_usage then + total_memory = total_memory + memory_usage + end + end +until cursor == 0 + +return total_memory \ No newline at end of file