From 194077694c9d2b42870eda5368130ecca6c42f3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Wed, 24 Apr 2024 17:44:38 +0200 Subject: [PATCH 1/9] PHPORM-174 Add doc for cache and locks --- docs/cache.txt | 234 ++++++++++++++++++++++++++++++ docs/includes/cache/migration.php | 14 ++ 2 files changed, 248 insertions(+) create mode 100644 docs/cache.txt create mode 100644 docs/includes/cache/migration.php diff --git a/docs/cache.txt b/docs/cache.txt new file mode 100644 index 000000000..d6bce4f82 --- /dev/null +++ b/docs/cache.txt @@ -0,0 +1,234 @@ +.. _laravel-queues: + +=============== +Cache and Locks +=============== + +.. facet:: + :name: genre + :values: tutorial + +.. meta:: + :keywords: php framework, cache, lock, code example + +Configuration +------------- + +In order to use MongoDB as backend for `Laravel Cache and Locks `__, +add a store configuration using the ``mongodb`` driver in ``config/cache.php``: + +.. code-block:: php + + 'stores' => [ + 'mongodb' => [ + 'driver' => 'mongodb', + 'connection' => 'mongodb', + 'collection' => 'cache', + 'lock_connection' => 'mongodb', + 'lock_collection' => 'cache_locks', + 'lock_lottery' => [2, 100], + 'lock_timeout' => 86400, + ], + ], + +The following table describes a list of cache and lock options +and their default values: + +.. list-table:: + :header-rows: 1 + :widths: 25 75 + + * - Setting + - Description + + * - ``driver`` + - **Required**. Specifies the lock driver to use. Must be ``mongodb``. + + * - ``connection`` + - **Required**. The database connection to use to store cache items. It must be a ``mongodb`` connection. + + * - ``collection`` + - Default ``cache``. Name of the MongoDB collection to store cache items. + + * - ``lock_connection`` + - Default to the cache ``connection``. The database connection to use to store locks. It must be a ``mongodb`` connection. + + * - ``lock_collection`` + - Default ``cache_locks``. Name of the MongoDB collection to store locks. + + * - ``lock_lottery`` + - Default ``[2, 100]``. Probability [chance, total] of pruning expired cache items. Set to [0, 0] to disable + + * - ``lock_timeout`` + - Default ``86400``. Time-to-live of the locks in seconds + + +Setup auto-expiration indexes +----------------------------- + +The :manual:`TTL indexes ` integrated into MongoDB automatically +delete documents when they have expired. Their use is optional with the ``mongodb`` +driver, but recommended as they provide better performance by delegating the +deletion of expired documents to MongoDB instead of requiring the application to +perform this task randomly. + +The best way is to create the indexes with a migration calling the methods +``createTTLIndex()`` provided by both the cache and the lock stores: + +.. literalinclude:: /includes/cache/migration.php + :language: php + :emphasize-lines: 11,12 + :dedent: + +Then run the migration: + +.. code-block:: none + + $ php artisan migrate + +Alternatively, the index can be created using :mdb-shell:`MongoDB Shell <>` (``mongosh``): + +.. code-block:: ts + + db.cache.createIndex( + /* Field that holds the expiration date */ + { expires_at: 1 }, + /* Delay to remove items after expiration */ + { expireAfterSeconds: 0 } + ) + +If you use Locks, disable ``lock_lottery`` by setting the probability to ``0``: + +.. code-block:: php + :highlightLines: [4] + + 'stores' => [ + 'mongodb' => [ + 'driver' => 'mongodb', + 'connection' => 'mongodb', + 'lock_lottery' => [0, 100], // Disabled + ], + ], + +Using MongoDB cache +------------------- + +The Laravel cache can be used to store any serializable data using the facade +``Illuminate\Support\Facades\Cache``: + +In this example: +- Gets the cache repository with the store ``mongodb``, +- Tries to read and return the cache item named ``foo``, +- If missing, calls the closure to compute the value, store this value forever and return it. + +..code-block:: php + + use Illuminate\Support\Facades\Cache; + + $value = Cache::store('mongodb')->get('foo', function () { + return [1, 2, 3]; + }); + +The default, cached objects do not expire. It is possible to define an expiry time. + +..code-block:: php + + Cache::store('mongodb')->set('foo', 'abc', '1 day'); + +Incrementing and decrementing a value is also supported, the value must +be initialized before. The following example initialize the counter to ``3``, +adds 5 and removes 2. + +..code-block:: php + + Cache::store('mongodb')->set('counter', 3); + Cache::store('mongodb')->increment('counter', 5); + Cache::store('mongodb')->decrement('counter', 2); + +.. note:: + + {+odm-short+} supports incrementing and decrementing with integer and float values. + +Configuring MongoDB as default cache +------------------------------------ + +To use the ``mongodb`` store by default, change the default store in +``config/cache.php``: + +.. code-block:: php + :emphasize-lines: 2 + + return [ + 'default' => env('CACHE_STORE', 'mongodb'), + + 'stores' => [ + 'mongodb' => [ + 'driver' => 'mongodb', + 'connection' => 'mongodb', + ], + ], + ]; + +The variable ``CACHE_STORE`` could be set in your environment or in +the ``.env`` file. Update or remove it: + +.. code-block:: none + + CACHE_STORE=mongodb + +Then you can use the ``Illuminate\Support\Facades\Cache`` facade and +automatic injection: + +.. code-block:: php + + use Illuminate\Support\Facades\Cache; + + Cache::get('foo', 5); + +This example shows how to use automatic injection of the cache +manager, using the default store. It is a controller that +increments a counter each time it is invoked. + + +.. code-block:: php + :emphasize-lines: 9,14 + + cache->increment('counter'); + } + } + + +Using MongoDB Lock +------------------ + +Atomic locks allow for the manipulation of distributed locks without worrying +about race conditions. + +..code-block:: php + :emphasize-lines: 3 + + use Illuminate\Support\Facades\Cache; + + $lock = Cache::store('mongodb')->lock('foo', 10); + + if ($lock->get()) { + // Lock acquired for 10 seconds... + + $lock->release(); + } + + diff --git a/docs/includes/cache/migration.php b/docs/includes/cache/migration.php new file mode 100644 index 000000000..cbb6d7783 --- /dev/null +++ b/docs/includes/cache/migration.php @@ -0,0 +1,14 @@ +createTTLIndex(); + $store->lock('')->createTTLIndex(); + } +}; From 1e42c9d6245a187e4caabbfc10e34dcb3c1fbea5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Thu, 25 Apr 2024 09:08:42 +0200 Subject: [PATCH 2/9] Jordan's review Co-authored-by: Jordan Smith <45415425+jordan-smith721@users.noreply.github.com> --- docs/cache.txt | 67 +++++++++++++++++++++++++------------------------- 1 file changed, 34 insertions(+), 33 deletions(-) diff --git a/docs/cache.txt b/docs/cache.txt index d6bce4f82..1928b100d 100644 --- a/docs/cache.txt +++ b/docs/cache.txt @@ -6,7 +6,7 @@ Cache and Locks .. facet:: :name: genre - :values: tutorial + :values: reference .. meta:: :keywords: php framework, cache, lock, code example @@ -14,8 +14,8 @@ Cache and Locks Configuration ------------- -In order to use MongoDB as backend for `Laravel Cache and Locks `__, -add a store configuration using the ``mongodb`` driver in ``config/cache.php``: +To use MongoDB as a backend for `Laravel Cache and Locks `__, +add a store configuration by using the ``mongodb`` driver in ``config/cache.php``: .. code-block:: php @@ -45,13 +45,13 @@ and their default values: - **Required**. Specifies the lock driver to use. Must be ``mongodb``. * - ``connection`` - - **Required**. The database connection to use to store cache items. It must be a ``mongodb`` connection. + - **Required**. The database connection used to store cache items. It must be a ``mongodb`` connection. * - ``collection`` - Default ``cache``. Name of the MongoDB collection to store cache items. * - ``lock_connection`` - - Default to the cache ``connection``. The database connection to use to store locks. It must be a ``mongodb`` connection. + - Default to the cache ``connection``. The database connection used to store locks. It must be a ``mongodb`` connection. * - ``lock_collection`` - Default ``cache_locks``. Name of the MongoDB collection to store locks. @@ -60,20 +60,20 @@ and their default values: - Default ``[2, 100]``. Probability [chance, total] of pruning expired cache items. Set to [0, 0] to disable * - ``lock_timeout`` - - Default ``86400``. Time-to-live of the locks in seconds + - Default ``86400``. Time-to-live of the locks, in seconds. -Setup auto-expiration indexes +Setup Auto-Expiration Indexes ----------------------------- The :manual:`TTL indexes ` integrated into MongoDB automatically -delete documents when they have expired. Their use is optional with the ``mongodb`` -driver, but recommended as they provide better performance by delegating the +delete documents when they expire. Their use is optional with the ``mongodb`` +driver, but recommended, because they provide better performance by delegating the deletion of expired documents to MongoDB instead of requiring the application to perform this task randomly. -The best way is to create the indexes with a migration calling the methods -``createTTLIndex()`` provided by both the cache and the lock stores: +Create the indexes with a migration that calls the +``createTTLIndex()`` methods provided by both the cache, and the lock stores: .. literalinclude:: /includes/cache/migration.php :language: php @@ -84,9 +84,9 @@ Then run the migration: .. code-block:: none - $ php artisan migrate + php artisan migrate -Alternatively, the index can be created using :mdb-shell:`MongoDB Shell <>` (``mongosh``): +Alternatively, you can create the index by using :mdb-shell:`MongoDB Shell <>` (``mongosh``): .. code-block:: ts @@ -110,18 +110,19 @@ If you use Locks, disable ``lock_lottery`` by setting the probability to ``0``: ], ], -Using MongoDB cache +Using MongoDB Cache ------------------- The Laravel cache can be used to store any serializable data using the facade -``Illuminate\Support\Facades\Cache``: +``Illuminate\Support\Facades\Cache``. -In this example: -- Gets the cache repository with the store ``mongodb``, -- Tries to read and return the cache item named ``foo``, -- If missing, calls the closure to compute the value, store this value forever and return it. +This example performs the following actions: -..code-block:: php +- Gets the cache repository with the ``mongodb`` store +- Tries to read and return the cache item named ``foo`` +- If missing, calls the closure to compute the value, stores the value forever, and returns it + +.. code-block:: php use Illuminate\Support\Facades\Cache; @@ -129,17 +130,17 @@ In this example: return [1, 2, 3]; }); -The default, cached objects do not expire. It is possible to define an expiry time. +By default, cached objects do not expire. However, it is possible to define an expiry time, as shown in the following example: -..code-block:: php +.. code-block:: php Cache::store('mongodb')->set('foo', 'abc', '1 day'); -Incrementing and decrementing a value is also supported, the value must -be initialized before. The following example initialize the counter to ``3``, -adds 5 and removes 2. +Incrementing and decrementing a value is also supported if the value is +initialized before. The following example initializes the counter to ``3``, +adds 5, and removes 2. -..code-block:: php +.. code-block:: php Cache::store('mongodb')->set('counter', 3); Cache::store('mongodb')->increment('counter', 5); @@ -149,7 +150,7 @@ adds 5 and removes 2. {+odm-short+} supports incrementing and decrementing with integer and float values. -Configuring MongoDB as default cache +Configuring MongoDB as Default Cache ------------------------------------ To use the ``mongodb`` store by default, change the default store in @@ -169,8 +170,8 @@ To use the ``mongodb`` store by default, change the default store in ], ]; -The variable ``CACHE_STORE`` could be set in your environment or in -the ``.env`` file. Update or remove it: +The variable ``CACHE_STORE`` can be set in your environment or in +the ``.env`` file. Update or remove it as follows: .. code-block:: none @@ -185,8 +186,8 @@ automatic injection: Cache::get('foo', 5); -This example shows how to use automatic injection of the cache -manager, using the default store. It is a controller that +The following example shows how to use automatic injection of the cache +manager by using the default store. The example creates a controller that increments a counter each time it is invoked. @@ -216,9 +217,9 @@ Using MongoDB Lock ------------------ Atomic locks allow for the manipulation of distributed locks without worrying -about race conditions. +about race conditions. The following example implements an atomic lock: -..code-block:: php +.. code-block:: php :emphasize-lines: 3 use Illuminate\Support\Facades\Cache; From 2a958dae7b35fc545338b4c9481c1761809e3da4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Thu, 25 Apr 2024 09:08:58 +0200 Subject: [PATCH 3/9] Apply suggestions from code review Co-authored-by: Jordan Smith <45415425+jordan-smith721@users.noreply.github.com> --- docs/cache.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/cache.txt b/docs/cache.txt index 1928b100d..c711d79af 100644 --- a/docs/cache.txt +++ b/docs/cache.txt @@ -57,7 +57,7 @@ and their default values: - Default ``cache_locks``. Name of the MongoDB collection to store locks. * - ``lock_lottery`` - - Default ``[2, 100]``. Probability [chance, total] of pruning expired cache items. Set to [0, 0] to disable + - Default ``[2, 100]``. Probability [chance, total] of pruning expired cache items. Set to [0, 0] to disable. * - ``lock_timeout`` - Default ``86400``. Time-to-live of the locks, in seconds. From e639b4747a45c62ae4b811d32470b84781503d6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Thu, 25 Apr 2024 10:59:07 +0200 Subject: [PATCH 4/9] Add links to Laravel docs for more details --- docs/cache.txt | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/docs/cache.txt b/docs/cache.txt index c711d79af..61e6689ac 100644 --- a/docs/cache.txt +++ b/docs/cache.txt @@ -31,6 +31,8 @@ add a store configuration by using the ``mongodb`` driver in ``config/cache.php` ], ], +To configure the ``mongodb`` database connection, see the :ref:`laravel-fundamentals-connection` section. + The following table describes a list of cache and lock options and their default values: @@ -57,7 +59,7 @@ and their default values: - Default ``cache_locks``. Name of the MongoDB collection to store locks. * - ``lock_lottery`` - - Default ``[2, 100]``. Probability [chance, total] of pruning expired cache items. Set to [0, 0] to disable. + - Default ``[2, 100]``. Probability ``[chance, total]`` of pruning expired cache items. Set to ``[0, 0]`` to disable. * - ``lock_timeout`` - Default ``86400``. Time-to-live of the locks, in seconds. @@ -150,6 +152,9 @@ adds 5, and removes 2. {+odm-short+} supports incrementing and decrementing with integer and float values. +For more information on using cache, see the `Laravel Cache documentation +`__. + Configuring MongoDB as Default Cache ------------------------------------ @@ -212,7 +217,6 @@ increments a counter each time it is invoked. } } - Using MongoDB Lock ------------------ @@ -232,4 +236,5 @@ about race conditions. The following example implements an atomic lock: $lock->release(); } - +For more information on using locks, see the `Laravel Locks documentation +`__. From e934c53fbf92c5ce97e3b25f6e6bc7c3182a2560 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Thu, 25 Apr 2024 20:02:43 +0200 Subject: [PATCH 5/9] Apply suggestions from code review --- docs/cache.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/cache.txt b/docs/cache.txt index 61e6689ac..abe3b6154 100644 --- a/docs/cache.txt +++ b/docs/cache.txt @@ -72,7 +72,7 @@ The :manual:`TTL indexes ` integrated into MongoDB automatical delete documents when they expire. Their use is optional with the ``mongodb`` driver, but recommended, because they provide better performance by delegating the deletion of expired documents to MongoDB instead of requiring the application to -perform this task randomly. +perform this task. Create the indexes with a migration that calls the ``createTTLIndex()`` methods provided by both the cache, and the lock stores: @@ -197,7 +197,7 @@ increments a counter each time it is invoked. .. code-block:: php - :emphasize-lines: 9,14 + :emphasize-lines: 8,13 Date: Thu, 25 Apr 2024 23:19:47 +0200 Subject: [PATCH 6/9] Apply suggestions from code review Co-authored-by: Jordan Smith <45415425+jordan-smith721@users.noreply.github.com> --- docs/cache.txt | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/docs/cache.txt b/docs/cache.txt index abe3b6154..eaaa8c280 100644 --- a/docs/cache.txt +++ b/docs/cache.txt @@ -15,7 +15,7 @@ Configuration ------------- To use MongoDB as a backend for `Laravel Cache and Locks `__, -add a store configuration by using the ``mongodb`` driver in ``config/cache.php``: +add a store configuration by specifying the ``mongodb`` driver in ``config/cache.php``: .. code-block:: php @@ -65,12 +65,12 @@ and their default values: - Default ``86400``. Time-to-live of the locks, in seconds. -Setup Auto-Expiration Indexes ------------------------------ +Set Up Auto-Expiration Indexes +------------------------------- The :manual:`TTL indexes ` integrated into MongoDB automatically delete documents when they expire. Their use is optional with the ``mongodb`` -driver, but recommended, because they provide better performance by delegating the +driver, but recommended. The indexes provide better performance by delegating the deletion of expired documents to MongoDB instead of requiring the application to perform this task. @@ -102,7 +102,7 @@ Alternatively, you can create the index by using :mdb-shell:`MongoDB Shell <>` ( If you use Locks, disable ``lock_lottery`` by setting the probability to ``0``: .. code-block:: php - :highlightLines: [4] + :emphasize-lines: 4 'stores' => [ 'mongodb' => [ @@ -152,11 +152,11 @@ adds 5, and removes 2. {+odm-short+} supports incrementing and decrementing with integer and float values. -For more information on using cache, see the `Laravel Cache documentation +For more information about using the cache, see the `Laravel Cache documentation `__. -Configuring MongoDB as Default Cache ------------------------------------- +Configuring MongoDB as the Default Cache +---------------------------------------- To use the ``mongodb`` store by default, change the default store in ``config/cache.php``: @@ -175,7 +175,7 @@ To use the ``mongodb`` store by default, change the default store in ], ]; -The variable ``CACHE_STORE`` can be set in your environment or in +The ``CACHE_STORE`` variable can be set in your environment or in the ``.env`` file. Update or remove it as follows: .. code-block:: none From a4ead2c0fbf463f3bc90a425e888fa2cf7f99972 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Fri, 26 Apr 2024 13:08:30 +0200 Subject: [PATCH 7/9] Add note on using default config --- docs/cache.txt | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/docs/cache.txt b/docs/cache.txt index eaaa8c280..5e14cf5c1 100644 --- a/docs/cache.txt +++ b/docs/cache.txt @@ -159,14 +159,13 @@ Configuring MongoDB as the Default Cache ---------------------------------------- To use the ``mongodb`` store by default, change the default store in -``config/cache.php``: +``config/cache.php``. .. code-block:: php :emphasize-lines: 2 return [ 'default' => env('CACHE_STORE', 'mongodb'), - 'stores' => [ 'mongodb' => [ 'driver' => 'mongodb', @@ -175,6 +174,11 @@ To use the ``mongodb`` store by default, change the default store in ], ]; +.. note:: + + We have deliberately omitted all optional parameters in the previous example, + so the default values are applied. + The ``CACHE_STORE`` variable can be set in your environment or in the ``.env`` file. Update or remove it as follows: From aa1dba8d026790d2303c270ff37fa946cf12a096 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Fri, 26 Apr 2024 17:10:25 +0200 Subject: [PATCH 8/9] Update docs/cache.txt Co-authored-by: Jordan Smith <45415425+jordan-smith721@users.noreply.github.com> --- docs/cache.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/cache.txt b/docs/cache.txt index 5e14cf5c1..b9176dd9f 100644 --- a/docs/cache.txt +++ b/docs/cache.txt @@ -201,7 +201,7 @@ increments a counter each time it is invoked. .. code-block:: php - :emphasize-lines: 8,13 + :emphasize-lines: 10,15 Date: Fri, 26 Apr 2024 17:49:10 +0200 Subject: [PATCH 9/9] Update navigation --- docs/cache.txt | 2 +- docs/index.txt | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/cache.txt b/docs/cache.txt index b9176dd9f..19609b94b 100644 --- a/docs/cache.txt +++ b/docs/cache.txt @@ -1,4 +1,4 @@ -.. _laravel-queues: +.. _laravel-cache: =============== Cache and Locks diff --git a/docs/index.txt b/docs/index.txt index 8513e9f15..b6b61b54d 100644 --- a/docs/index.txt +++ b/docs/index.txt @@ -20,6 +20,7 @@ Laravel MongoDB /eloquent-models /query-builder /user-authentication + /cache /queues /transactions /issues-and-help @@ -69,6 +70,7 @@ see the following content: - :ref:`laravel-query-builder` - :ref:`laravel-aggregation-builder` - :ref:`laravel-user-authentication` +- :ref:`laravel-cache` - :ref:`laravel-queues` - :ref:`laravel-transactions`