I stored a lot of stuff in Redis. One group of them are with namespace cache
(key starts with cache:
). I want to know the size of the data/values with namespace cache
. Can I achieve this in Redis? Any suggestions?
I stored a lot of stuff in Redis. One group of them are with namespace cache
(key starts with cache:
). I want to know the size of the data/values with namespace cache
. Can I achieve this in Redis? Any suggestions?
3
Answers
You may use scan with memory usage commands. Depending on the size of your database(you may check it with DBSIZE) – you may arrange the
count
option of thescan
command. The following command is going to scan the database with matching to thecache:
prefix.Then you may execute
MEMORY USAGE
on the individual keys. You can achieve it in your favorite programming language with available redis library.The lua example could be something like this(i don’t have enough experience on lua but it looks like working). It is going to return total size of the values in bytes.
it may not be the best "performing" solution for large databases. you may need to update your cursor.
Edit:
As @for_stack pointed out in the comment, it will not work when the count is less than your total key size when the count is less, it needs to be iterated multiple times.
You can do this with RedisGears (https://oss.redislabs.com/redisgears/) with a single line:
The first map operation get the size of each key and the aggregate operation sums it. the argument to the run function is the keys prefix to run on.
nodejs snippet using ioredis: