@@ -888,11 +888,16 @@ First, create a service that will compute the item's value::
888888 // src/Cache/CacheComputation.php
889889 namespace App\Cache;
890890
891- use Symfony\Contracts\Cache\ItemInterface;
892-
893- class CacheComputation
891+ use Psr\Cache\CacheItemInterface;
892+ use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
893+ use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
894+ use Symfony\Contracts\Cache\CallbackInterface;
895+
896+ #[Autoconfigure(tags: ['container.reversible'], public: true)]
897+ #[AutoconfigureTag]
898+ class CacheComputation implements CallbackInterface
894899 {
895- public function compute(ItemInterface $item): string
900+ public function __invoke(CacheItemInterface $item, bool &$save ): string
896901 {
897902 $item->expiresAfter(5);
898903
@@ -916,10 +921,10 @@ In the following example, the value is requested from a controller::
916921 class CacheController extends AbstractController
917922 {
918923 #[Route('/cache', name: 'cache')]
919- public function index(CacheInterface $asyncCache): Response
924+ public function index(CacheInterface $asyncCache, CacheComputation $cacheComputation ): Response
920925 {
921926 // pass to the cache the service method that refreshes the item
922- $cachedValue = $asyncCache->get('my_value', [CacheComputation::class, 'compute'] )
927+ $cachedValue = $asyncCache->get('my_value', $cacheComputation )
923928
924929 // ...
925930 }
0 commit comments