You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
< ? php
namespace App\Console\Commands ; // 定义命名空间
use App\Enums\HomeCacheEnum ; // 引入 HomeCacheEnum 枚举
use App\Models\Category ; // 引入 Category 模型
use App\Models\Product ; // 引入 Product 模型
use App\Models\User ; // 引入 User 模型
use App\Utils\HomeCacheDataUtil ; // 引入 HomeCacheDataUtil 工具类
use Carbon\Carbon ; // 引入 Carbon 日期处理库
use Illuminate\Console\Command ; // 引入 Laravel 的 Command 基类
use Illuminate\Support\Facades\Cache ; // 引入 Cache 门面
/**
* 更新首页缓存数据命令
*
* 该命令用于更新首页的缓存数据,包括分类、热销商品、最新商品和用户信息。
*/
class UpdateCacheHomeData extends Command
{
/**
* 控制台命令的名称和签名
*
* @var string
*/
protected $signature = 'moon:update-home' ; // 定义命令的名称和签名
/**
* 控制台命令的描述
*
* @var string
*/
protected $description = '更新首页缓存数据' ; // 描述该命令的用途
/**
* 创建一个新的命令实例.
*
* @return void
*/
public function __construct ()
{
parent :: __construct (); // 调用父类构造函数
}
/**
* 执行控制台命令
*
* 该方法执行更新缓存数据的逻辑,定时更新首页所需的各类数据。
*
* @return mixed
*/
public function handle ()
{
// 设置缓存的生存时间( TTL) , 单位为秒, 这里设置为 2 分钟
$ttl = 60 * 2 ; // 每 2 分钟更新一次缓存
// 更新分类缓存数据
HomeCacheDataUtil :: categories ( $ttl , true ); // 更新分类数据并设置 TTL
// 更新热销商品缓存数据
HomeCacheDataUtil :: hotProducts ( $ttl , true ); // 更新热销商品数据并设置 TTL
// 更新最新商品缓存数据
HomeCacheDataUtil :: latestProducts ( $ttl , true ); // 更新最新商品数据并设置 TTL
// 更新用户信息缓存数据
HomeCacheDataUtil :: users ( $ttl , true ); // 更新用户数据并设置 TTL
}
}