定制 spinen/laravel-garbage-man 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

spinen/laravel-garbage-man

最新稳定版本:2.5.0

Composer 安装命令:

composer require spinen/laravel-garbage-man

包简介

Scheduled job to clean out Laravel's soft deleted records at configured interval.

README 文档

README

Latest Stable Version Latest Unstable Version Total Downloads License

The soft deletes are great in Laravel to make sure that some deleted data can be recovered. This package allows you to configure an array of models with how many days that you want the soft deleted data to stay in the database.

Build Status

Branch Status Coverage Code Quality
Develop Build Status Code Coverage Scrutinizer Code Quality
Master Build Status Code Coverage Scrutinizer Code Quality

Prerequisite

NOTE: If you need to use < PHP 7.2, please stay with version 1.x

As side from Laravel >= 5.1.10 (5.1.10 is the first version that had the warn method, so that is the minimum for logging), there is 1 package that is required.

Install

Install Garbage Man:

$ composer require spinen/laravel-garbage-man

For >= Laravel 5.5, you are done with the installation

The package uses the auto registration feature of Laravel 5.

Upgrading to 2.x from 1.x

As of Laravel 5.8 (and deprecated in 5.4), the fire() method on the dispatcher contract was removed in favor of dispatch(). Therefore, we have updated our code to use dispatch(). You will need to change fire() to dispatch() in your config/garbageman.php file.

Using the command

The command is registered with laravel as garbageman:purge. You can run it one of 2 ways...

  1. from the command line php artisan garbageman:purge;
  2. via a scheduled task.

To automatically run the script as a scheduled job, then add the following to the schedule method of App\Console\Kernel.php:

$schedule->command('garbageman:purge')
         ->daily();

You can use whatever schedule that you need to keep the records purged out. Just review the list at http://laravel.com/docs/master/scheduling#schedule-frequency-options.

You can also use any of the advanced configuration options of the task scheduler like "Task Output" or "Task Hooks" as listed on the Laravel documentation.

Configuration

Publish the package config file to config/garbageman.php:

$ php artisan vendor:publish

This file is fully documented. You will need to make the changes to that file to suit your needs. There are 3 main configuration items...

  1. Dispatch purge events - Dispatch events on purge of each record.
  2. Logging level - Level to log.
  3. Schedule - Models & number of days to allow the soft deleted record to stay before purging.

Dispatch purge events (dispatch_purge_events)

You may hook into the purge of each record by throwing events before & after deleting of each record. There are 2 events thrown:

  • garbageman.purging:<full/model/name>
  • garbageman.purged:<full/model/name>

The model is passed with each of the events. The "purging" event is thrown just before the actual delete & "purged" is thrown just after the actual deletion.

Please note: This is an expensive operation as it requires a SQL command for each record to delete so that the record can be thrown with the events. Therefore, unless you need to catch the events to perform some other action, leave this false to allow all records per model to get deleted with a single SQL call.

Logging level (logging_level)

The level that log messages are generated, which will display information on the console output and in the logs.

Level Description
0 Emergency: system is unusable
1 Alert: action must be taken immediately
2 Critical: critical conditions
3 Error: error conditions
4 Warning: warning conditions
5 Notice: normal but significant condition
6 (default) Info: informational messages
7 Debug: debug - level messages

There is a key for the console & one for the log. Here is an example...

'logging_level' => [
    'console' => 3,
    'log'     => 6,
],

Alternatively, you can set the levels with environmental variables GARBAGEMAN_CONSOLE_LOG_LEVEL and GARBAGEMAN_LOG_LEVEL.

Schedule (schedule)

The age is in days for each model. Here is an example...

'schedule' => [
    App\ModelOne::class => 14,
    App\ModelTwo::class => 30,
],

This would purge any ModelOnes that were deleted over 14 days ago and any ModelTwos that were deleted over 30 days ago.

spinen/laravel-garbage-man 适用场景与选型建议

spinen/laravel-garbage-man 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19.04k 次下载、GitHub Stars 达 33, 最近一次更新时间为 2015 年 11 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「library」 「softdelete」 「laravel」 「spinen」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 spinen/laravel-garbage-man 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 spinen/laravel-garbage-man 我们能提供哪些服务?
定制开发 / 二次开发

基于 spinen/laravel-garbage-man 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 19.04k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 36
  • 点击次数: 21
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 33
  • Watchers: 10
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-11-03