A
A
Alexander Gamov2019-06-09 18:58:22
Redis
Alexander Gamov, 2019-06-09 18:58:22

Why is the data not being retrieved from the cache?

There is an api written in lumen. A request for a bunch of orders generates a large amount of data (during the resource assembly, a bunch of data is pulled up, including raw requests to the database). It was decided to cache each order, and radish was chosen as the cache driver.

$orderIds = $request->list;

// Получаем ордера из кеша
$cachedOrders = Cache::tags($orderIds)->get('orders-resource') ?? [];

// Ищем айдишники, которых в кеше не было
$nonCachedIds = count($cachedOrders) === 0 ? $orderIds : $cachedOrders->whereNotIn('id', $orderIds);

// Получаем из базы не закешированные ордера
$fromDbOrders =  Order::withTrashed()->with('comments')->findMany($nonCachedIds);

foreach ($fromDbOrders as $order) {
    $orderResourced = OrderResource::make($order)->toArray($request);
    $cachedOrders[] = $orderResourced;
    Cache::tags($order->id)->put(
        "orders-resource",
        $orderResourced,
        Carbon::SECONDS_PER_MINUTE * 10000
    );
}

return response()->json(new Report(
    "200",
    $cachedOrders,
    "Список заказов для базы данных",
    $requestId
));

In radish, I see them, but on the next request, I try to display cached orders through dd($cachedOrders);I get emptiness. Perhaps I misunderstand the concept of tags? it will be difficult to do without them, because. Several caches will be linked to one order and there will be a need to clear everything related to a specific order.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Gamov, 2019-06-26
@slowdream

Tags are not suitable for this application. I use it only to clear the cache by tags.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question