简体   繁体   English

Laravel 5.3 - 防止太多通知?

[英]Laravel 5.3 - prevent too many notifications?

My laravel 5.3 application creates database notifications for users when a photo was commented or 'liked'.当照片被评论或“喜欢”时,我的 laravel 5.3 应用程序会为用户创建数据库通知。

If something is liked and unliked often, the user would receive lots of notifications although there is, for example, only 1 new Like .如果经常喜欢和不喜欢某样东西,那么用户会收到很多通知,尽管例如只有 1 个新的Like

Perhaps the notification function could sleep() for 5 minutes, then check if the Like or Comment still exists, before creating the notification.也许通知 function 可以sleep() 5 分钟,然后在创建通知之前检查点LikeComment是否仍然存在。 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? laravel 中是否已经包含某些内容(队列、“锁定”列或表)或其他简单的解决方案?

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. 如果上次通知时间+ 5分钟<当前时间-发送新通知。

You can delay the job in Laravel with delay(): https://laravel.com/docs/5.2/queues#delayed-jobs 您可以使用delay()在Laravel中延迟工作: https ://laravel.com/docs/5.2/queues#delayed-jobs

And to avoid sending multiple notifications I would use xpuc7o's approach. 为了避免发送多个通知,我将使用xpuc7o的方法。 To make things faster I would store it in memcache and not db. 为了使事情更快,我将其存储在memcache中而不是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.这个想法是使用具有自定义特征RoutesThrottledNotifications的 Laravel RateLimitter class 来限制某些通知的速率并避免重复。

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更多信息和代码在这里: https://scottwakefield.co.uk/writing/rate-limiting-notifications-in-laravel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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