Rockstar Rate Limiting ๐ŸŽธ๐Ÿฅ๐ŸŽ‰

Funny Laravel docs · Warning: May cause actual learning AND laughter!

Browse funny laravel docs
On this page

Welcome to the groovy land of controlled chaos! Hereโ€™s your guide on how to keep the party going while still keeping a handle on things ๐Ÿค˜.

Cache Configuration: The Secret Sauce ๐Ÿ”๐Ÿฅ„

Before we dive into the dance floor, letโ€™s set up our secret sauceโ€”the cache configuration! Get your dancing shoes ready because itโ€™s gonna be a hoot. ๐Ÿ•บ๐Ÿ’ƒ

Basic Usage: The First Dance ๐Ÿ•บ๐Ÿฟ๏ธ๐Ÿ’ƒ๐Ÿฟ๏ธ

Time to hit the floor and learn the moves! Hereโ€™s a step-by-step guide on how to do it right. ๐Ÿ’ฅ

Manually Incrementing Attempts: Show โ€˜em you mean business ๐Ÿค๐Ÿฟ๏ธ๐Ÿ’ช๐Ÿฟ๏ธ

Feeling frisky? Want to prove your mettle? This is where you show off those impressive math skills and manually increment those attempts. We dare ya! ๐Ÿฅณ

Clearing Attempts: Time for a rewind โฎ๏ธ๐Ÿ’ƒ

Oh no, someone stepped on your foot? No worries, itโ€™s time to clear the dance floor and start anew! This section will teach you how. ๐Ÿคฏ๐Ÿคช

Hello, Web Wranglers! ๐Ÿค“

Welcome to Laravelโ€™s Rate Limiting Playground! ๐Ÿš€

Weโ€™ve cooked up a super-simple, laughably easy rate limiting abstraction thatโ€™ll make your life easier than a well-buttered bagel. Pair it with your appโ€™s cache (weโ€™re talking about the coffee-fueled caching, not the leftover pizza in the fridge), and youโ€™ve got an effortless solution to limit any action within a time window thatโ€™ll make you say โ€œTime flies when youโ€™re having fun!โ€ ๐Ÿ•ฐ๏ธ

Note

If rate limiting incoming HTTP requests is more your jam, weโ€™ve got the docs for that. Check out our rate limiter middleware documentation.

Now, letโ€™s set up the kitchen (errโ€ฆ cache configuration). ๐Ÿ› ๏ธ๐Ÿš€

Unleash the Power of Caching, Comrades! (or: How I Learned to Stop Worrying and Love the Cache)

Ahoy there, fearless coder! Prepare thyself for a dazzling dance with the mystical realm of Laravelโ€™s cache configuration. But fear not, this journey wonโ€™t be as dull as a night spent watching paint dry - we promise some laughs along the way!

First off, you may have noticed that our dear rate limiter usually depends on your trusty application cache (defined by the default key in your applicationโ€™s cache configuration file). However, if you fancy changing horses mid-stream, you can dictate which cache driver the rate limiter should hitch its wagon to by defining a limiter key within the same hallowed document:

'default' => env('CACHE_STORE', 'database'),

'limiter' => 'redis', // [tl! add]

Aw, shucks, we almost forgot to make this a barrel of laughs! So letโ€™s imagine for a moment that our rate limiter is a wayward cowboy, and the cache drivers are his trusty steeds. The default setting saddles him up with a trusty olโ€™ horse called โ€˜databaseโ€™. But now youโ€™ve got yourself a fancy new horse named โ€˜redisโ€™, and our cowboy just canโ€™t resist the allure of that shiny saddle!

And so, the rodeo begins anew! May your caching be swift and your code never run aground. Keep on codinโ€™, partners! ๐Ÿค ๐ŸŽ๐Ÿš€

Ahoy there, matey! Buckle up as we delve into the world of the Illuminate\Support\Facades\RateLimiter - a pirateโ€™s treasure chest full of rate limiting fun! This facade is your gateway to interact with our shipโ€™s mighty rate limiter.

The swashbuckling method it offers that weโ€™ll focus on today is the attempt โ€“ a versatile tool used to limit the number of times a particular action (or โ€œcallbackโ€ in proper speak) can be executed within a given time frame, measured in seconds.

The attempt method works like this: when it runs out of remaining attempts for a specific callback, it returns false. Otherwise, itโ€™ll return either the callbackโ€™s result or true. The first argument that the attempt method accepts is the โ€œkeyโ€ โ€“ a string representing the action being rate limited. For instance, to limit sending messages:

use Illuminate\Support\Facades\RateLimiter;

$pirateSpeak = 'arrr';
if ($executed = RateLimiter::attempt(
    'send-message:' . $user->id . $pirateSpeak, // Combine user ID and a "pirate greeting" for fun!
    5, // Allow 5 messages per minute
    function() {
        // Send message to the parrot... or rather, the user.
    }
)) {
    if (!$executed) {
        return 'Arrr matey, too many messages sent!';
    }
}

If you find yourself in need of adjusting the time when attempts reset (also known as โ€œdecay rateโ€), you can provide a fourth argument to the attempt method. Letโ€™s make our example allow five attempts every two minutes:

$pirateSpeak = 'arrgh';
$executed = RateLimiter::attempt(
    'send-message:' . $user->id . $pirateSpeak, // Update the pirate greeting for added flavor!
    5, // Allow 5 messages per two minutes
    function() {
        // Send message to the parrot... or rather, the user.
    },
    120 // Reset attempts every 120 seconds (2 minutes)
);

Now that youโ€™ve learned the basics of our rate limiterโ€™s attempt, you can set sail on your journey to becoming a true rate limiting maestro! Yo ho, yo ho, a pirateโ€™s life for me!

Alright, buckle up, code cowboys and cowgirls! Letโ€™s dive into the wild west of rate limiting in Laravel land.

Yeehaw-ally Manual Rate Wrangling

If you fancy some old-fashioned human interaction with our trusty rate limiter sheriff, youโ€™ve come to the right corral! With a twang in our voice and a smirk on our face, letโ€™s see how we can round up some methods:

use Illuminate\Support\Facades\RateLimiter;

If you think your message horse has been overworked (`tooManyAttempts` method), just give it a lasso check:

```bash
if (RateLimiter::tooManyAttempts('send-message:'.$user->id, $perMinute = 5)) {
    return 'Whoa there! This message horse has outrun its six-shootin' quota!';
}

Give your message horse a fresh carrot (`increment` method):

```bash
RateLimiter::increment('send-message:'.$user->id);

// Time to send that message, partner!

Fancy counting the remaining carrots for your message horse? Look no further than our trusty remaining method:

if (RateLimiter::remaining('send-message:'.$user->id, $perMinute = 5)) {
    RateLimiter::increment('send-message:'.$user->id);

    // Send that message, cowpoke!
}

Need to feed your message horse a big olโ€™ carrot bouquet? No problemo, pass the desired amount to our increment method:

RateLimiter::increment('send-message:'.$user->id, amount: 5);

Now, letโ€™s rope in some more knowledge on determining limiter availability: (Just kidding, weโ€™re all out of horse metaphors for now.)

To learn more about the available rate limiters and their settings, consult our trusty Rate Limiting documentation. Happy trailblazing! ๐Ÿค ๐ŸŽ

Alright, letโ€™s dive into the thrilling world of rate limiting in Laravel, where weโ€™re not just coding, but conducting a symphony of digital throttling!

When your key (we like to call them โ€œdigital drumsticksโ€) reaches its limit, the availableIn method steps up as our timekeeper, telling us how many seconds are left until the band can start playing again:

Use Illuminate\Support\Facades\RateLimiter;

If RateLimiter::drumStickBash('send-message:' . $user->id, $perMinute = 5) is more like a drum solo than a duet,
    $seconds = RateLimiter::countdownToJam('send-message:' . $user->id);

    Return 'Give us a beat in '.$seconds.' seconds. Your digital drumsticks will be ready to roll again!';
}

RateLimiter::hitTheDrums('send-message:' . $user->id);

// Send message...

Now, if youโ€™re wondering how to clear those attempts after the jam session, fear not! Hereโ€™s how:

If you need to clear all attempts for a specific key (because letโ€™s be honest, sometimes we all need a clean slate):

RateLimiter::clear('send-message:' . $user->id);

But wait! Thereโ€™s more! If you want to clear just the locked attempts (we donโ€™t want to throw away the good times, do we?), use this:

RateLimiter::forget('send-message:' . $user->id);

And there you have it! Now youโ€™re not only a Laravel maestro, but also a digital drum circle leader, ensuring your users never get drummed out of the fun. Rock on!

Unleashing the Beast

Oh, you fancy yourself a limerick maestro? Well, grab your lariat and letโ€™s gallop into the wild west of rate limiting!

In this frontier town, weโ€™ve got a tool called clear, Thatโ€™ll reset attempts for each key that you hold dear. No more bottlenecks, no more delays, Just like the cavalry, itโ€™ll chase those troubles away!

Use yer six-shooters, App's models and signs,
Import Message and Facade of RateLimiter's designs.

When a message is read with a twirl of the hat,
Mark it as read, and clear that key won't regret.

RateLimiter::clear('send-message:'.$message->user_id)
Will ensure all attempts, like tumbleweeds, will abide.

Just remember, pardner, this ainโ€™t just a fancy dance, Youโ€™re handling data, so keep your code well-anchored!

Our network