How to use cache clear without resetting RateLimiter #54305
-
Hey everyone, I'm looking to use Laravel's RateLimiter to rate limit event triggers and notification triggers. The issue : RateLimiter uses cache, which gets cleared whenever I deploy in production (to prevent any issues with previous versions). This causes an issue if I want to limit the number of notification to 1 per day for example. Here is a concrete example : Let's imagine I have a Ratelimiter configured to 1 per day : What I'de love to achieve is exactly what RateLimiter is doing in cache but persisted, without reinventing the wheel. Any tips ? Thanks ! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Solved it on my own. When using The easy solution was to create a new DB connection that uses redis db 2. In my case, I called it // config/database.php
// ..
'redis' => [
// ...
'cache' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_CACHE_DB', '1'),
],
'persistent' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_PERSISTED_DB', '2'),
], Then create a new cache store that uses this connection : // config/cache.php
//...
'stores' => [
//...
'redis' => [
'driver' => 'redis',
'connection' => 'cache',
'lock_connection' => 'default',
],
'persistent_redis' => [
'driver' => 'redis',
'connection' => 'persistent',
'lock_connection' => 'default',
],
//... Finally, in the same file, configure the rate limiter per laravel's documentation to use this store rather than the default one : // config/cache.php
<?php
use Illuminate\Support\Str;
return [
'default' => env('CACHE_DRIVER', 'file'),
'limiter' => env('LIMITER_DRIVER', 'persistent_redis'),
//...
While this allows me to use I hope this can help other people someday :) Cheers ! |
Beta Was this translation helpful? Give feedback.
Solved it on my own.
For anyone looking for an answer to this :
When using
php artisan cache:clear
, Laravel actually only clears the default cache. This means that it will only clear the "cache" connection configured, which uses db 1.The easy solution was to create a new DB connection that uses redis db 2. In my case, I called it
persistent
: