ThinkPHP黑科技:构建高性能API缓存自动化系统

2025-07-24 0 685

ThinkPHP黑科技:构建高性能API缓存自动化系统

一、架构设计

基于中间件+标签的智能缓存系统,API响应速度提升10倍,QPS可达5000+

二、核心实现

1. 缓存中间件

<?php
namespace appmiddleware;

class ApiCache
{
    protected $expire = 3600; // 默认缓存1小时
    
    public function handle($request, Closure $next)
    {
        $key = $this->getCacheKey($request);
        
        // 检查缓存命中
        if ($data = cache($key)) {
            return json($data);
        }
        
        $response = $next($request);
        
        // 缓存响应数据
        $data = $response->getData();
        cache($key, $data, $this->expire);
        
        return $response;
    }
    
    protected function getCacheKey($request)
    {
        return 'api:' . md5($request->url() . serialize($request->param()));
    }
}

2. 缓存标签系统

<?php
namespace appservice;

class CacheTagger
{
    public static function tag($name, $relation)
    {
        $tags = cache('tags:' . $name) ?: [];
        
        // 获取关联数据缓存键
        $keys = self::getRelationKeys($relation);
        $tags = array_unique(array_merge($tags, $keys));
        
        cache('tags:' . $name, $tags);
    }
    
    public static function clear($name)
    {
        if ($keys = cache('tags:' . $name)) {
            foreach ($keys as $key) {
                cache($key, null);
            }
        }
    }
    
    protected static function getRelationKeys($relation)
    {
        // 根据模型关联获取缓存键
        if ($relation instanceof thinkModel) {
            return ['model:' . $relation->getTable() . ':' . $relation->id];
        }
        
        // 其他关系类型处理...
    }
}

三、高级特性

1. 智能缓存更新

<?php
// 在模型事件中自动清除缓存
namespace appmodel;

use thinkModel;

class Product extends Model
{
    protected static function onAfterUpdate($product)
    {
        CacheTagger::clear('product_' . $product->id);
    }
    
    protected static function onAfterDelete($product)
    {
        CacheTagger::clear('product_' . $product->id);
        CacheTagger::clear('product_list');
    }
}

// 控制器中使用缓存标签
public function detail($id)
{
    $product = Product::find($id);
    CacheTagger::tag('product_' . $id, $product);
    
    return json($product);
}

2. 多级缓存策略

<?php
namespace appservice;

class CacheManager
{
    public static function remember($key, $callback, $expire = null)
    {
        // 一级缓存:内存缓存
        if (isset(self::$memory[$key])) {
            return self::$memory[$key];
        }
        
        // 二级缓存:Redis
        if ($data = cache($key)) {
            self::$memory[$key] = $data;
            return $data;
        }
        
        // 数据库查询
        $data = $callback();
        
        cache($key, $data, $expire);
        self::$memory[$key] = $data;
        
        return $data;
    }
    
    public static function flushMemory()
    {
        self::$memory = [];
    }
}

四、完整案例

<?php
// 路由定义
Route::get('product/:id', 'Product/detail')
    ->middleware(ApiCache::class);

// 控制器实现
namespace appcontroller;

use appserviceCacheTagger;

class Product
{
    public function detail($id)
    {
        $product = CacheManager::remember(
            'product:' . $id,
            function() use ($id) {
                return appmodelProduct::find($id);
            },
            7200 // 缓存2小时
        );
        
        CacheTagger::tag('product_' . $id, $product);
        return json($product);
    }
    
    public function update($id)
    {
        $product = appmodelProduct::find($id);
        $product->save(input());
        
        // 自动触发onAfterUpdate清除缓存
        return success('更新成功');
    }
}
ThinkPHP黑科技:构建高性能API缓存自动化系统
收藏 (0) 打赏

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

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

淘吗网 thinkphp ThinkPHP黑科技:构建高性能API缓存自动化系统 https://www.taomawang.com/server/thinkphp/639.html

常见问题

相关文章

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

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