ThinkPHP6.2全链路性能优化:从请求到响应的极致加速方案

2025-07-14 0 819

ThinkPHP6.2全链路性能优化:从请求到响应的极致加速方案

一、请求处理层优化

基于中间件的智能缓存预热策略:

// app/middleware/CachePreheat.php
namespace appmiddleware;

class CachePreheat
{
    public function handle($request, Closure $next)
    {
        $key = 'route:cache:' . md5($request->url());
        
        // 热门请求预加载
        if (redis()->zscore('hot:requests', $request->url()) > 100) {
            if ($data = cache($key)) {
                return response($data);
            }
            
            // 标记正在处理 (防击穿)
            cache($key, 'processing', 3);
        }
        
        $response = $next($request);
        
        // 缓存热门响应
        if (redis()->zscore('hot:requests', $request->url()) > 50) {
            cache($key, $response->getContent(), 3600);
        }
        
        return $response;
    }
}

// 注册中间件 app/middleware.php
return [
    appmiddlewareCachePreheat::class
];

核心优势:智能预判零延迟响应防缓存击穿动态适应

二、数据库优化策略

1. 智能查询构造器

// 扩展查询构造器 app/common/QueryBuilder.php
class QueryBuilder extends thinkdbQuery
{
    public function cached($ttl = 60, $key = null)
    {
        if (!$key) {
            $key = 'sql:' . md5($this->getLastSql());
        }
        
        if ($data = cache($key)) {
            return $data;
        }
        
        $result = $this->select();
        cache($key, $result, $ttl);
        
        return $result;
    }
    
    public function withCacheTags(array $tags)
    {
        $this->options['cache_tags'] = $tags;
        return $this;
    }
}

// 使用示例
Db::name('user')
    ->where('status', 1)
    ->withCacheTags(['user_list'])
    ->cached(300);

2. 分库分表中间件

// 分表策略 app/common/Sharding.php
class UserSharding
{
    public static function getTable($userId)
    {
        $suffix = $userId % 10;
        return "user_{$suffix}";
    }
    
    public static function getDb($userId)
    {
        $suffix = floor($userId / 1000000) % 3;
        return "user_db_{$suffix}";
    }
}

// 查询时自动路由
Db::connect(UserSharding::getDb($userId))
    ->table(UserSharding::getTable($userId))
    ->where('user_id', $userId)
    ->find();

三、高并发实战案例

1. 秒杀系统三级缓存

// app/service/SeckillService.php
class SeckillService
{
    public function checkStock($productId)
    {
        // 1. 本地缓存
        if ($stock = LocalCache::get("stock_{$productId}")) {
            return $stock > 0;
        }
        
        // 2. Redis缓存
        if ($stock = redis()->get("seckill:stock:{$productId}")) {
            LocalCache::set("stock_{$productId}", $stock, 5);
            return $stock > 0;
        }
        
        // 3. 数据库查询
        $stock = Db::name('product')
            ->where('id', $productId)
            ->value('stock');
            
        redis()->set("seckill:stock:{$productId}", $stock, 60);
        LocalCache::set("stock_{$productId}", $stock, 5);
        
        return $stock > 0;
    }
    
    public function createOrder($userId, $productId)
    {
        // Redis原子递减
        $stock = redis()->decr("seckill:stock:{$productId}");
        if ($stock  $userId,
            'product_id' => $productId
        ]);
        
        return true;
    }
}

四、全链路监控体系

  • SQL监控:Db::getQueryTimes()分析慢查询
  • 请求追踪:中间件记录各阶段耗时
  • 缓存命中率:Redis统计各缓存层效果
  • 压力测试:ab/wrk模拟高并发场景
  • 性能看板:Prometheus+Grafana可视化监控
ThinkPHP6.2全链路性能优化:从请求到响应的极致加速方案
收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

淘吗网 thinkphp ThinkPHP6.2全链路性能优化:从请求到响应的极致加速方案 https://www.taomawang.com/server/thinkphp/328.html

下一篇:

已经没有下一篇了!

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务