@@ -6,16 +6,16 @@ Cache and Locks
6
6
7
7
.. facet::
8
8
:name: genre
9
- :values: tutorial
9
+ :values: reference
10
10
11
11
.. meta::
12
12
:keywords: php framework, cache, lock, code example
13
13
14
14
Configuration
15
15
-------------
16
16
17
- In order to use MongoDB as backend for `Laravel Cache and Locks <https://laravel.com/docs/{+laravel-docs-version+}/cache>`__,
18
- add a store configuration using the ``mongodb`` driver in ``config/cache.php``:
17
+ To use MongoDB as a backend for `Laravel Cache and Locks <https://laravel.com/docs/{+laravel-docs-version+}/cache>`__,
18
+ add a store configuration by using the ``mongodb`` driver in ``config/cache.php``:
19
19
20
20
.. code-block:: php
21
21
@@ -45,13 +45,13 @@ and their default values:
45
45
- **Required**. Specifies the lock driver to use. Must be ``mongodb``.
46
46
47
47
* - ``connection``
48
- - **Required**. The database connection to use to store cache items. It must be a ``mongodb`` connection.
48
+ - **Required**. The database connection used to store cache items. It must be a ``mongodb`` connection.
49
49
50
50
* - ``collection``
51
51
- Default ``cache``. Name of the MongoDB collection to store cache items.
52
52
53
53
* - ``lock_connection``
54
- - Default to the cache ``connection``. The database connection to use to store locks. It must be a ``mongodb`` connection.
54
+ - Default to the cache ``connection``. The database connection used to store locks. It must be a ``mongodb`` connection.
55
55
56
56
* - ``lock_collection``
57
57
- Default ``cache_locks``. Name of the MongoDB collection to store locks.
@@ -60,20 +60,20 @@ and their default values:
60
60
- Default ``[2, 100]``. Probability [chance, total] of pruning expired cache items. Set to [0, 0] to disable
61
61
62
62
* - ``lock_timeout``
63
- - Default ``86400``. Time-to-live of the locks in seconds
63
+ - Default ``86400``. Time-to-live of the locks, in seconds.
64
64
65
65
66
- Setup auto-expiration indexes
66
+ Setup Auto-Expiration Indexes
67
67
-----------------------------
68
68
69
69
The :manual:`TTL indexes </core/index-ttl/>` integrated into MongoDB automatically
70
- delete documents when they have expired . Their use is optional with the ``mongodb``
71
- driver, but recommended as they provide better performance by delegating the
70
+ delete documents when they expire . Their use is optional with the ``mongodb``
71
+ driver, but recommended, because they provide better performance by delegating the
72
72
deletion of expired documents to MongoDB instead of requiring the application to
73
73
perform this task randomly.
74
74
75
- The best way is to create the indexes with a migration calling the methods
76
- ``createTTLIndex()`` provided by both the cache and the lock stores:
75
+ Create the indexes with a migration that calls the
76
+ ``createTTLIndex()`` methods provided by both the cache, and the lock stores:
77
77
78
78
.. literalinclude:: /includes/cache/migration.php
79
79
:language: php
@@ -84,9 +84,9 @@ Then run the migration:
84
84
85
85
.. code-block:: none
86
86
87
- $ php artisan migrate
87
+ php artisan migrate
88
88
89
- Alternatively, the index can be created using :mdb-shell:`MongoDB Shell <>` (``mongosh``):
89
+ Alternatively, you can create the index by using :mdb-shell:`MongoDB Shell <>` (``mongosh``):
90
90
91
91
.. code-block:: ts
92
92
@@ -110,36 +110,37 @@ If you use Locks, disable ``lock_lottery`` by setting the probability to ``0``:
110
110
],
111
111
],
112
112
113
- Using MongoDB cache
113
+ Using MongoDB Cache
114
114
-------------------
115
115
116
116
The Laravel cache can be used to store any serializable data using the facade
117
- ``Illuminate\Support\Facades\Cache``:
117
+ ``Illuminate\Support\Facades\Cache``.
118
118
119
- In this example:
120
- - Gets the cache repository with the store ``mongodb``,
121
- - Tries to read and return the cache item named ``foo``,
122
- - If missing, calls the closure to compute the value, store this value forever and return it.
119
+ This example performs the following actions:
123
120
124
- ..code-block:: php
121
+ - Gets the cache repository with the ``mongodb`` store
122
+ - Tries to read and return the cache item named ``foo``
123
+ - If missing, calls the closure to compute the value, stores the value forever, and returns it
124
+
125
+ .. code-block:: php
125
126
126
127
use Illuminate\Support\Facades\Cache;
127
128
128
129
$value = Cache::store('mongodb')->get('foo', function () {
129
130
return [1, 2, 3];
130
131
});
131
132
132
- The default, cached objects do not expire. It is possible to define an expiry time.
133
+ By default, cached objects do not expire. However, it is possible to define an expiry time, as shown in the following example:
133
134
134
- ..code-block:: php
135
+ .. code-block:: php
135
136
136
137
Cache::store('mongodb')->set('foo', 'abc', '1 day');
137
138
138
- Incrementing and decrementing a value is also supported, the value must
139
- be initialized before. The following example initialize the counter to ``3``,
140
- adds 5 and removes 2.
139
+ Incrementing and decrementing a value is also supported if the value is
140
+ initialized before. The following example initializes the counter to ``3``,
141
+ adds 5, and removes 2.
141
142
142
- ..code-block:: php
143
+ .. code-block:: php
143
144
144
145
Cache::store('mongodb')->set('counter', 3);
145
146
Cache::store('mongodb')->increment('counter', 5);
@@ -149,7 +150,7 @@ adds 5 and removes 2.
149
150
150
151
{+odm-short+} supports incrementing and decrementing with integer and float values.
151
152
152
- Configuring MongoDB as default cache
153
+ Configuring MongoDB as Default Cache
153
154
------------------------------------
154
155
155
156
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
169
170
],
170
171
];
171
172
172
- The variable ``CACHE_STORE`` could be set in your environment or in
173
- the ``.env`` file. Update or remove it:
173
+ The variable ``CACHE_STORE`` can be set in your environment or in
174
+ the ``.env`` file. Update or remove it as follows :
174
175
175
176
.. code-block:: none
176
177
@@ -185,8 +186,8 @@ automatic injection:
185
186
186
187
Cache::get('foo', 5);
187
188
188
- This example shows how to use automatic injection of the cache
189
- manager, using the default store. It is a controller that
189
+ The following example shows how to use automatic injection of the cache
190
+ manager by using the default store. The example creates a controller that
190
191
increments a counter each time it is invoked.
191
192
192
193
@@ -216,9 +217,9 @@ Using MongoDB Lock
216
217
------------------
217
218
218
219
Atomic locks allow for the manipulation of distributed locks without worrying
219
- about race conditions.
220
+ about race conditions. The following example implements an atomic lock:
220
221
221
- ..code-block:: php
222
+ .. code-block:: php
222
223
:emphasize-lines: 3
223
224
224
225
use Illuminate\Support\Facades\Cache;
0 commit comments