9

I've installed a web app called scrumblr which uses redis as a database.

What I am attempting to do is delete all keys that have been inactive for 30 days, or have not been accessed in 30 days.

I ran

redis-cli KEYS*

Which returns all of the keys, though it does not show a timestamp.

Is there a script or a command I can run each day at a specific time, which would seek out all the inactive keys and delete them?

Muji Sayed
  • 91
  • 1
  • 1
  • 3

2 Answers2

11

You may make take the advantage of OBJECT IDLETIME command, which returns the number of seconds since the object stored at the specified key is idle (not requested by read or write operations).

Example code as follows:

#!/bin/sh

redis-cli -p 6379 keys "*" | while read LINE ;
do
val=`redis-cli -p 6379 object idletime $LINE`;
if [ $val -gt $((30 * 24 * 60 * 60)) ];
then
  echo "$LINE";
  # del=`redis-cli -p 6379 del $LINE`;  # be careful with del
  # echo $del;
fi
done;

In your situation, you can replace redis-cli -p 6379 with:

redis-cli -h redis_host -p redis_port -a redis_password

fibonacci
  • 975
  • 8
  • 8
3

Short answer: no.

Long answer: you can iterate over your keys (the output of KEYS - do not use that command in production! Use SCAN instead), call for each key the OBJECT IDLETIME command and delete based on the response.

Longer answer: you can actually change scrumplr's source to have Redis automatically expire keys after 30 days. The suspect file appears to be lib/data/redis.js and the patch requires simply using the SET...EX or EXPIRE commands on each key after it is written to (room, board, card).

HalosGhost
  • 4,732
  • 10
  • 33
  • 41
Itamar Haber
  • 131
  • 3