@@ -843,29 +843,25 @@ Computing Cache Values Asynchronously
843843
844844.. versionadded :: 5.2
845845
846- Computing cache values asynchronously with the Messenger
847- in a worker was introduced in Symfony 5.2.
848-
849- Combined with the :doc: `Messenger component docs </components/messenger >`, the
850- Cache component allows you to compute and refresh cache values asynchronously.
851-
852- The :class: `Symfony\\ Contracts\\ Cache\\ CacheInterface ` enables
853- `probabilistic early expiration `_, which means that sometimes, items are
854- elected for early-expiration while they are still fresh. You can learn more
855- about it in the :ref: `cache stampede prevention <cache_stampede-prevention >`
856- section.
857-
858- Under classical circumstances, expired cache items are computed synchronously.
859- However, with a bit of additional configuration, values computation can be
860- delegated to a background worker. In this case, when an item is queried,
861- its cached value is immediately returned and a
846+ The feature to compute cache values asynchronously was introduced in Symfony 5.2.
847+
848+ The Cache component uses the `probabilistic early expiration `_ algorithm to
849+ protect against the :ref: `cache stampede <cache_stampede-prevention >` problem.
850+ This means that some cache items are elected for early-expiration while they are
851+ still fresh.
852+
853+ By default, expired cache items are computed synchronously. However, you can
854+ compute them asynchronously by delegating the value computation to a background
855+ worker using the :doc: `Messenger component </components/messenger >`. In this case,
856+ when an item is queried, its cached value is immediately returned and a
862857:class: `Symfony\\ Component\\ Cache\\ Messenger\\ EarlyExpirationMessage ` is
863- dispatched through a Messenger bus. When this message is handled by a
864- message consumer, the refreshed cache value is computed asynchronously.
865- The next time the item is queried, the refreshed value will be fresh
866- and returned.
858+ dispatched through a Messenger bus.
867859
868- First, let's declare a service that will compute the item's value::
860+ When this message is handled by a message consumer, the refreshed cache value is
861+ computed asynchronously. The next time the item is queried, the refreshed value
862+ will be fresh and returned.
863+
864+ First, create a service that will compute the item's value::
869865
870866 // src/Cache/CacheComputation.php
871867 namespace App\Cache;
@@ -878,11 +874,13 @@ First, let's declare a service that will compute the item's value::
878874 {
879875 $item->expiresAfter(5);
880876
877+ // this is just a random example; here you must do your own calculation
881878 return sprintf('#%06X', mt_rand(0, 0xFFFFFF));
882879 }
883880 }
884881
885- Now, we can create a controller that will query this item::
882+ This cache value will be requested from a controller, another service, etc.
883+ In the following example, the value is requested from a controller::
886884
887885 // src/Controller/CacheController.php
888886 namespace App\Controller;
@@ -900,15 +898,15 @@ Now, we can create a controller that will query this item::
900898 */
901899 public function index(CacheInterface $asyncCache): Response
902900 {
903- // we give to the cache the service method that refreshes the item
901+ // pass to the cache the service method that refreshes the item
904902 $cachedValue = $cache->get('my_value', [CacheComputation::class, 'compute'])
905903
906904 // ...
907905 }
908906 }
909907
910- Finally, we configure a new cache pool called ``async.cache `` that will use a
911- message bus to compute values in a worker:
908+ Finally, configure a new cache pool (e.g. called ``async.cache ``) that will use
909+ a message bus to compute values in a worker:
912910
913911.. configuration-block ::
914912
@@ -931,7 +929,7 @@ message bus to compute values in a worker:
931929
932930 <!-- config/packages/framework.xml -->
933931 <?xml version =" 1.0" encoding =" UTF-8" ?>
934- <container xmlns="http://symfony.com/schema/dic/services"
932+ <container xmlns =" http://symfony.com/schema/dic/services"
935933 xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
936934 xmlns : framework =" http://symfony.com/schema/dic/symfony"
937935 xsi : schemaLocation =" http://symfony.com/schema/dic/services
@@ -979,7 +977,8 @@ You can now start the consumer:
979977 $ php bin/console messenger:consume async_bus
980978
981979 That's it! Now, whenever an item is queried from this cache pool, its cached
982- value will be immediately returned. If it is elected for early-expiration, a message is sent
983- through to bus to schedule a background computation to refresh the value.
980+ value will be returned immediately. If it is elected for early-expiration, a
981+ message will be sent through to bus to schedule a background computation to refresh
982+ the value.
984983
985984.. _`probabilistic early expiration` : https://en.wikipedia.org/wiki/Cache_stampede#Probabilistic_early_expiration
0 commit comments