Skip to content

Commit

Permalink
demo redis quota
Browse files Browse the repository at this point in the history
  • Loading branch information
msciabarra committed May 11, 2024
1 parent c2c8790 commit 2fdb668
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
17 changes: 17 additions & 0 deletions demos/demo-redis-quota/README.md
Original file line number Diff line number Diff line change
@@ -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 <prefix> <n>

Then you can check providing the prefixes to check (as I assume we know them )""

bash check.sh <prefx> <prefix>...

Example

```
bash fill.sh mike 100
bash check.sh mike franz
bash fill.sh franz 200
bash check.sh mike franz
```
5 changes: 5 additions & 0 deletions demos/demo-redis-quota/check.sh
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions demos/demo-redis-quota/fill.sh
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions demos/demo-redis-quota/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
sudo apt-get install redis-server redis-tools
sudo systemctl start redis-server
sudo systemctl status redis-server

21 changes: 21 additions & 0 deletions demos/demo-redis-quota/size_by_prefix.lua
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2fdb668

Please sign in to comment.