There is now a helper for this exact scenario
Jan 14, 2025
6
Level 2
Recreate the cache with a job
I'm developing an ecommerce with thousands of products. The show page of the products is cached in this way
$showProduct = Cache::remember('product_show_' . $product->id, 60 * 60 * 24, function () use ($product) {
return [
"id" => $product->id,
"title" => $product->title,
"content" => $product->content,
"excerpt" => $product->excerpt,
....
];
});
When the cache is cleaned or expired the first user to visit the single product page gets a certain delay that i would like to remove.
To achieve this my idea is to use a Job that every 12 hours restores the cache.
public function handle()
{
Product::where('status', 'publish')->chunk(15, function($products){
// Iterate over the products
foreach($products as $product){
(new productController)->getProduct($product);
}
});
Unfortunately, if I run this job my script uses all the memory available to PHP ( 2GB ). My understanding is that this is because the scheduled JOB is using the same process for all the cache creations. How to start different processes?
Please or to participate in this conversation.