简体   繁体   中英

Laravel 5.3 - prevent too many notifications?

My laravel 5.3 application creates database notifications for users when a photo was commented or 'liked'.

If something is liked and unliked often, the user would receive lots of notifications although there is, for example, only 1 new Like .

Perhaps the notification function could sleep() for 5 minutes, then check if the Like or Comment still exists, before creating the notification. But then I still must somehow 'lock' it to not have too many notifications created(?).

Is there something already included in laravel (queues, 'lock'-column or table), or another simple solution?

Thanks for advice.

I think better solution would be to set an offset somewhere in your code (some class) or in database.

Than before send a notification you can check:

if last notification time + 5 min < current time - send a new notification.

You can delay the job in Laravel with delay(): https://laravel.com/docs/5.2/queues#delayed-jobs

And to avoid sending multiple notifications I would use xpuc7o's approach. To make things faster I would store it in memcache and not db.

I know that this is an old question, but I found a better approach to resolve this.

The idea is to use the Laravel RateLimitter class with a custom trait RoutesThrottledNotifications in order to rate limit certain notifications an avoid duplications.

This way you just have to set a throttle decay time in each notification that you want to rate limit.

More info and code here: https://scottwakefield.co.uk/writing/rate-limiting-notifications-in-laravel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM