Array

14 views
Skip to first unread message

Ronald Wiplinger

unread,
Feb 20, 2018, 3:32:51 AM2/20/18
to Redis DB
I'm starting with Redis and need some advices.

I have chat bot, which responds to trigger words in my database (keyword, hint). However, the chat bot is too active. I want to slow it down with Redis.

My idea is to add a keyword, that the chat bot answered into Redis with a TTL of 60 min. There are maybe 100 keywords used in the past hour. My php program should check if that keyword is in Redis. Since it should be kept for 60 min, I will only check if that keyword is there, if it is, skip the answer, if not, post the answer.

Could I use a list, to add these keywords? I have so far only learned that Redis keys can expire, but I'm unsure if it could do also on elements of a list.

Geoffrey Hoffman

unread,
Feb 20, 2018, 8:55:12 AM2/20/18
to redi...@googlegroups.com
The key containing the entire list can have an expiry, but not individual list members.

Jean-Michel Hiver

unread,
Feb 20, 2018, 9:22:24 AM2/20/18
to Redis DB
You can't do with a list but you could use a ZSET to emulate what you want. Just add your keyword in the ZSET with the timestamp as score. Something like ZADD recenly_used_keywords $timestamp $keyword.

Before reading from the ZSET, make sure you clean it by removing stuff that's too old using ZREMRANGEBYSCORE.

Once that's done, use ZSCORE to find out if your element is still in there or not. If you have a timestamp, it's there, if you get Nil, it isn't.
Reply all
Reply to author
Forward
0 new messages