I want to have an array in Redis (using Node) where I can add values to it and specify how long I want it to stay in there. After that time limit, they should be deleted, and ideally be able to call something so I know what just left. ex. I may get a request with 120s, so I want to add that value to a map for that long and then have it deleted.
我想在Redis中使用一个数组(使用Node),我可以在其中添加值并指定我希望它保留多久。在那个时间限制之后,它们应该被删除,理想情况下可以调用一些东西,所以我知道刚才剩下的东西。恩。我可能会得到一个120s的请求,所以我想将该值添加到地图上那么久,然后将其删除。
Is there a better way to do this? I thought of using the EXPIRE but that seems to be just for keys, not elements in an array?
有一个更好的方法吗?我想过使用EXPIRE,但这似乎只是用于键,而不是数组中的元素?
Any thoughts would be great.
任何想法都会很棒。
This is what I am doing:
这就是我在做的事情:
app.get('/session/:length', function(req, res) {
var length = parseInt(req.param('length'), 10);
addToArray(length, ip)
var ip = req.connection.remoteAddress;
res.json({ip: ip, length: length});
});
Basically, I when I add it to the array I want it to only keep it in the array for the time that is passed in. So if you say 30 seconds, it's in that array for 30s, and then is gone, and calls a callback. Maybe there is a better way to solve this problem?
基本上,当我将它添加到数组时,我希望它只在传入的时间内将它保留在数组中。所以如果你说30秒,它就在那个数组中30秒,然后就消失了,并调用了回电话。也许有更好的方法来解决这个问题?
What I do now is keep the times added and ip, time in an array and periodically loop through the array checking and deleting, but thought maybe it would be possible in redis to automatically do this.
我现在做的是保持添加的时间和IP,时间在一个数组中,并定期循环数组检查和删除,但想想也许在redis中可以自动执行此操作。
1 个解决方案
#1
2
While there isn't an automatic way to do that in Redis, the common approach to these kind of problems is to use a Redis sorted set. In your case, set the IP as the member's value and the expiry time (now + time to live) as the score using epoch representation.
虽然Redis中没有自动的方法,但是这类问题的常见方法是使用Redis排序集。在您的情况下,使用纪元表示将IP设置为成员的值和到期时间(现在+生存时间)作为分数。
Instead of looping periodically, you can just call ZREMRANGEBYSCORE every once in a while.
您可以每隔一段时间调用ZREMRANGEBYSCORE,而不是定期循环。
Since set members are unique, however, that means that you'll only be able to save each IP once. If that's OK, just update the score for an IP with every hit from it, otherwise make the member value unique by concatenating the IP with the timestamp.
但是,由于集合成员是唯一的,这意味着您只能保存每个IP一次。如果没有问题,只需更新每次点击的IP分数,否则通过将IP与时间戳连接来使成员值唯一。
Lastly, to get the IPs that haven't "expired", use ZRANGEBYSCORE to get members that have scores (expiry times) higher than now. Similarly and before deleting with ZREMRANGEBYSCORE, get the keys that expired for the callback logic that you mentioned.
最后,要获得未“过期”的IP,请使用ZRANGEBYSCORE来获得分数(到期时间)高于现在的成员。类似地,在使用ZREMRANGEBYSCORE删除之前,获取您提到的回调逻辑的过期密钥。
#1
2
While there isn't an automatic way to do that in Redis, the common approach to these kind of problems is to use a Redis sorted set. In your case, set the IP as the member's value and the expiry time (now + time to live) as the score using epoch representation.
虽然Redis中没有自动的方法,但是这类问题的常见方法是使用Redis排序集。在您的情况下,使用纪元表示将IP设置为成员的值和到期时间(现在+生存时间)作为分数。
Instead of looping periodically, you can just call ZREMRANGEBYSCORE every once in a while.
您可以每隔一段时间调用ZREMRANGEBYSCORE,而不是定期循环。
Since set members are unique, however, that means that you'll only be able to save each IP once. If that's OK, just update the score for an IP with every hit from it, otherwise make the member value unique by concatenating the IP with the timestamp.
但是,由于集合成员是唯一的,这意味着您只能保存每个IP一次。如果没有问题,只需更新每次点击的IP分数,否则通过将IP与时间戳连接来使成员值唯一。
Lastly, to get the IPs that haven't "expired", use ZRANGEBYSCORE to get members that have scores (expiry times) higher than now. Similarly and before deleting with ZREMRANGEBYSCORE, get the keys that expired for the callback logic that you mentioned.
最后,要获得未“过期”的IP,请使用ZRANGEBYSCORE来获得分数(到期时间)高于现在的成员。类似地,在使用ZREMRANGEBYSCORE删除之前,获取您提到的回调逻辑的过期密钥。