php 如何缩短Laravel API响应时间?

vu8f3i0k  于 5个月前  发布在  PHP
关注(0)|答案(3)|浏览(57)

我是用这种方法提取数据的,但是当我用Postman查询时,得到响应的时间太长了。

public function getAllItems(Request $request)
    {
        $type = $request->input('type');
        
        $query = Item::query();
        
        if ($type === 'series') {
            $query->where('is_series', true);
        } elseif ($type === 'movies') {
            $query->where('is_series', false);
        }

        $items = $query->get();
        
        return response()->json($items, 200, [], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    }

字符串
我试着使用缓存来缩短响应时间,还在响应中添加了查询的开始和结束时间。

public function getAllItems(Request $request)
{
    $type = $request->input('type');

    $cacheKey = 'all_items_' . $type;

    $startTime = microtime(true);

    if (Cache::has($cacheKey)) {
        $items = Cache::get($cacheKey);
        
        $endTime = microtime(true);
        $executionTime = ($endTime - $startTime);
        
        return response()->json([
            'isCache' => true,
            'time' => $executionTime . ' seconds',
            'data' => $items,
            ], 200, [], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    } else {
        $query = Item::query();
        
        if ($type === 'series') {
            $query->where('is_series', true);
        } elseif ($type === 'movies') {
            $query->where('is_series', false);
        }

        $items = $query->get();

        Cache::put($cacheKey, $items, now()->addMinutes(60));
        
        $endTime = microtime(true);
        $executionTime = ($endTime - $startTime);
        
        return response()->json([
            'isCache' => false,
            'time' => $executionTime . ' seconds',
            'data' => $items,
            ], 200, [], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    }
}


的数据
然而,正如您在屏幕截图中所看到的,我添加到响应中的时间与Postman返回响应所用的时间之间存在较大差异。“
我如何才能获得更快的响应?

4uqofj5v

4uqofj5v1#

要解决这个问题,有几种方法:首先,使用select方法忽略查询中的额外列,其次,使用分页。

r6hnlfcb

r6hnlfcb2#

有几种方法可以减少API响应。
1.使用select方法。这样它将只选择你需要的列。
范例:

$items = $query->select('id', 'name', 'description', 'created_at', 'updated_at')->get();

字符串
1.对“is_series”列使用索引,因为您正在此列上使用条件。
1.您的缓存密钥可能会重叠。请确保您的缓存密钥是唯一的。
1.考虑对大型数据集进行分页
例如

$query->paginate(10);


以上几点将缩短您的响应时间。

vlf7wbxs

vlf7wbxs3#

在我的例子中,我实现了以下问题的解决方案:
我使用“toArray()”方法将数组添加到该高速缓存中。因此,当它在该高速缓存中获取数据时,它不会浪费时间再次解析它。
我将数据无限期地保存在该高速缓存中,并在必要时使用另一个函数更新该缓存。
以下是最终版本的样子:

public function getAllItems(Request $request)
{
    $type = $request->input('type');
    
    $cacheKey = 'all_items_' . $type;
    
    if (Cache::has($cacheKey)) {
        $items = Cache::get($cacheKey);
        
        return response()->json(['data' => $items], 200, [], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    } else {
        $query = Item::query();
        
        if ($type === 'series') {
            $query->where('is_series', true);
        } elseif ($type === 'movies') {
            $query->where('is_series', false);
        }
        
        $items = $query->get();

        Cache::forever($cacheKey, $items->toArray());

        return response()->json(['data' => $items], 200, [], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    }
}
public function updateCache(Request $request)
{
    $type = $request->input('type');
    $cacheKey = 'all_items_' . $type;
    
    $query = Item::query();

    if ($type === 'series') {
        $query->where('is_series', true);
    } elseif ($type === 'movies') {
        $query->where('is_series', false);
    }
    
    $items = $query->get();
    
    Cache::forever($cacheKey, $items->toArray());
    
    return response()->json([
            'status' => true,
            'message' => $type . ' Cache updated successfully',
            'statusCode' => 200
            ], 200);
}

字符串

相关问题