jeudi 7 janvier 2016

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. (I use 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. 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.

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.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire