[Cache] Wrong callable in async cache computation example

This commit is contained in:
Marvin Bölsterli
2025-11-24 13:06:12 +01:00
committed by Javier Eguiluz
parent 643bc71597
commit dcd2a1230f

View File

@@ -888,11 +888,12 @@ First, create a service that will compute the item's value::
// src/Cache/CacheComputation.php
namespace App\Cache;
use Symfony\Contracts\Cache\ItemInterface;
use Psr\Cache\CacheItemInterface;
use Symfony\Contracts\Cache\CallbackInterface;
class CacheComputation
class CacheComputation implements CallbackInterface
{
public function compute(ItemInterface $item): string
public function __invoke(CacheItemInterface $item, bool &$save): string
{
$item->expiresAfter(5);
@@ -916,10 +917,10 @@ In the following example, the value is requested from a controller::
class CacheController extends AbstractController
{
#[Route('/cache', name: 'cache')]
public function index(CacheInterface $asyncCache): Response
public function index(CacheInterface $asyncCache, CacheComputation $cacheComputation): Response
{
// pass to the cache the service method that refreshes the item
$cachedValue = $asyncCache->get('my_value', [CacheComputation::class, 'compute'])
$cachedValue = $asyncCache->get('my_value', $cacheComputation)
// ...
}