[英]How to get all cache entries with a tag on Laravel
I'm building a login throttling system using Laravel, which I use to save every failed login on a cache Database. 我正在使用Laravel构建登录限制系统,我用它来保存缓存数据库上的每个失败登录。 (I use Redis).
(我使用Redis)。
The code: 编码:
class FailedLogins
{
const NUM_FAILURES_TO_LOCK = 30,
TIME_RANGE = 10; // In minutes
public function add($email, $ip = null)
{
if (is_null($ip))
$ip = request()->ip();
$index = md5($email . $ip);
Cache::tags('failed.logins')->put($index, 1, self::TIME_RANGE);
}
public function hasTooMany()
{
$numFailedLogins = count(Cache::tags('failed.logins')->get());
return ($numFailedLogins >= self::NUM_FAILURES_TO_LOCK);
}
}
The issue is on the hasTooMany
method, I have to provide a key parameter on the get
method. 问题出在
hasTooMany
方法上,我必须在get
方法上提供一个关键参数。 What I was trying to do on this line: Cache::tags('failed.logins')->get()
is to get all entries with the failed.logins
tag, so I can count how many there are. 我试图在这一行上做什么:
Cache::tags('failed.logins')->get()
是获取带有failed.logins
标记的所有条目,所以我可以计算有多少条目。
Well, that is not working, because I can't do that. 好吧,那不行,因为我做不到。 So what do you recommend me to use so I can solve it?
那你建议我用什么来解决呢? If it's a Redis only solutions that's fine too.
如果它只是一个Redis解决方案也没关系。
You could use redis hashes: 你可以使用redis哈希:
http://redis.io/commands/hset http://redis.io/commands/hset
But you can't set individual expiration date on hash keys, so you would have to delete them manually, or use main key with hour in it, like: failed.logins:08 and expire whole. 但是您无法在哈希键上设置单独的到期日期,因此您必须手动删除它们,或者使用带有小时的主键,例如:failed.logins:08和expire whole。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.