@@ -843,29 +843,25 @@ Computing Cache Values Asynchronously
843
843
844
844
.. versionadded :: 5.2
845
845
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
862
857
: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.
867
859
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::
869
865
870
866
// src/Cache/CacheComputation.php
871
867
namespace App\Cache;
@@ -878,11 +874,13 @@ First, let's declare a service that will compute the item's value::
878
874
{
879
875
$item->expiresAfter(5);
880
876
877
+ // this is just a random example; here you must do your own calculation
881
878
return sprintf('#%06X', mt_rand(0, 0xFFFFFF));
882
879
}
883
880
}
884
881
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::
886
884
887
885
// src/Controller/CacheController.php
888
886
namespace App\Controller;
@@ -900,15 +898,15 @@ Now, we can create a controller that will query this item::
900
898
*/
901
899
public function index(CacheInterface $asyncCache): Response
902
900
{
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
904
902
$cachedValue = $cache->get('my_value', [CacheComputation::class, 'compute'])
905
903
906
904
// ...
907
905
}
908
906
}
909
907
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:
912
910
913
911
.. configuration-block ::
914
912
@@ -931,7 +929,7 @@ message bus to compute values in a worker:
931
929
932
930
<!-- config/packages/framework.xml -->
933
931
<?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"
935
933
xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
936
934
xmlns : framework =" http://symfony.com/schema/dic/symfony"
937
935
xsi : schemaLocation =" http://symfony.com/schema/dic/services
@@ -979,7 +977,8 @@ You can now start the consumer:
979
977
$ php bin/console messenger:consume async_bus
980
978
981
979
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.
984
983
985
984
.. _`probabilistic early expiration` : https://en.wikipedia.org/wiki/Cache_stampede#Probabilistic_early_expiration
0 commit comments