|
| 1 | +.. _laravel-queues: |
| 2 | + |
| 3 | +=============== |
| 4 | +Cache and Locks |
| 5 | +=============== |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: tutorial |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: php framework, cache, lock, code example |
| 13 | + |
| 14 | +Configuration |
| 15 | +------------- |
| 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``: |
| 19 | + |
| 20 | +.. code-block:: php |
| 21 | + |
| 22 | + 'stores' => [ |
| 23 | + 'mongodb' => [ |
| 24 | + 'driver' => 'mongodb', |
| 25 | + 'connection' => 'mongodb', |
| 26 | + 'collection' => 'cache', |
| 27 | + 'lock_connection' => 'mongodb', |
| 28 | + 'lock_collection' => 'cache_locks', |
| 29 | + 'lock_lottery' => [2, 100], |
| 30 | + 'lock_timeout' => 86400, |
| 31 | + ], |
| 32 | + ], |
| 33 | + |
| 34 | +The following table describes a list of cache and lock options |
| 35 | +and their default values: |
| 36 | + |
| 37 | +.. list-table:: |
| 38 | + :header-rows: 1 |
| 39 | + :widths: 25 75 |
| 40 | + |
| 41 | + * - Setting |
| 42 | + - Description |
| 43 | + |
| 44 | + * - ``driver`` |
| 45 | + - **Required**. Specifies the lock driver to use. Must be ``mongodb``. |
| 46 | + |
| 47 | + * - ``connection`` |
| 48 | + - **Required**. The database connection to use to store cache items. It must be a ``mongodb`` connection. |
| 49 | + |
| 50 | + * - ``collection`` |
| 51 | + - Default ``cache``. Name of the MongoDB collection to store cache items. |
| 52 | + |
| 53 | + * - ``lock_connection`` |
| 54 | + - Default to the cache ``connection``. The database connection to use to store locks. It must be a ``mongodb`` connection. |
| 55 | + |
| 56 | + * - ``lock_collection`` |
| 57 | + - Default ``cache_locks``. Name of the MongoDB collection to store locks. |
| 58 | + |
| 59 | + * - ``lock_lottery`` |
| 60 | + - Default ``[2, 100]``. Probability [chance, total] of pruning expired cache items. Set to [0, 0] to disable |
| 61 | + |
| 62 | + * - ``lock_timeout`` |
| 63 | + - Default ``86400``. Time-to-live of the locks in seconds |
| 64 | + |
| 65 | + |
| 66 | +Setup auto-expiration indexes |
| 67 | +----------------------------- |
| 68 | + |
| 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 |
| 72 | +deletion of expired documents to MongoDB instead of requiring the application to |
| 73 | +perform this task randomly. |
| 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: |
| 77 | + |
| 78 | +.. literalinclude:: /includes/cache/migration.php |
| 79 | + :language: php |
| 80 | + :emphasize-lines: 11,12 |
| 81 | + :dedent: |
| 82 | + |
| 83 | +Then run the migration: |
| 84 | + |
| 85 | +.. code-block:: none |
| 86 | + |
| 87 | + $ php artisan migrate |
| 88 | + |
| 89 | +Alternatively, the index can be created using :mdb-shell:`MongoDB Shell <>` (``mongosh``): |
| 90 | + |
| 91 | +.. code-block:: ts |
| 92 | + |
| 93 | + db.cache.createIndex( |
| 94 | + /* Field that holds the expiration date */ |
| 95 | + { expires_at: 1 }, |
| 96 | + /* Delay to remove items after expiration */ |
| 97 | + { expireAfterSeconds: 0 } |
| 98 | + ) |
| 99 | + |
| 100 | +If you use Locks, disable ``lock_lottery`` by setting the probability to ``0``: |
| 101 | + |
| 102 | +.. code-block:: php |
| 103 | + :highlightLines: [4] |
| 104 | + |
| 105 | + 'stores' => [ |
| 106 | + 'mongodb' => [ |
| 107 | + 'driver' => 'mongodb', |
| 108 | + 'connection' => 'mongodb', |
| 109 | + 'lock_lottery' => [0, 100], // Disabled |
| 110 | + ], |
| 111 | + ], |
| 112 | + |
| 113 | +Using MongoDB cache |
| 114 | +------------------- |
| 115 | + |
| 116 | +The Laravel cache can be used to store any serializable data using the facade |
| 117 | +``Illuminate\Support\Facades\Cache``: |
| 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. |
| 123 | + |
| 124 | +..code-block:: php |
| 125 | + |
| 126 | + use Illuminate\Support\Facades\Cache; |
| 127 | + |
| 128 | + $value = Cache::store('mongodb')->get('foo', function () { |
| 129 | + return [1, 2, 3]; |
| 130 | + }); |
| 131 | + |
| 132 | +The default, cached objects do not expire. It is possible to define an expiry time. |
| 133 | + |
| 134 | +..code-block:: php |
| 135 | + |
| 136 | + Cache::store('mongodb')->set('foo', 'abc', '1 day'); |
| 137 | + |
| 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. |
| 141 | + |
| 142 | +..code-block:: php |
| 143 | + |
| 144 | + Cache::store('mongodb')->set('counter', 3); |
| 145 | + Cache::store('mongodb')->increment('counter', 5); |
| 146 | + Cache::store('mongodb')->decrement('counter', 2); |
| 147 | + |
| 148 | +.. note:: |
| 149 | + |
| 150 | + {+odm-short+} supports incrementing and decrementing with integer and float values. |
| 151 | + |
| 152 | +Configuring MongoDB as default cache |
| 153 | +------------------------------------ |
| 154 | + |
| 155 | +To use the ``mongodb`` store by default, change the default store in |
| 156 | +``config/cache.php``: |
| 157 | + |
| 158 | +.. code-block:: php |
| 159 | + :emphasize-lines: 2 |
| 160 | + |
| 161 | + return [ |
| 162 | + 'default' => env('CACHE_STORE', 'mongodb'), |
| 163 | + |
| 164 | + 'stores' => [ |
| 165 | + 'mongodb' => [ |
| 166 | + 'driver' => 'mongodb', |
| 167 | + 'connection' => 'mongodb', |
| 168 | + ], |
| 169 | + ], |
| 170 | + ]; |
| 171 | + |
| 172 | +The variable ``CACHE_STORE`` could be set in your environment or in |
| 173 | +the ``.env`` file. Update or remove it: |
| 174 | + |
| 175 | +.. code-block:: none |
| 176 | + |
| 177 | + CACHE_STORE=mongodb |
| 178 | + |
| 179 | +Then you can use the ``Illuminate\Support\Facades\Cache`` facade and |
| 180 | +automatic injection: |
| 181 | + |
| 182 | +.. code-block:: php |
| 183 | + |
| 184 | + use Illuminate\Support\Facades\Cache; |
| 185 | + |
| 186 | + Cache::get('foo', 5); |
| 187 | + |
| 188 | +This example shows how to use automatic injection of the cache |
| 189 | +manager, using the default store. It is a controller that |
| 190 | +increments a counter each time it is invoked. |
| 191 | + |
| 192 | + |
| 193 | +.. code-block:: php |
| 194 | + :emphasize-lines: 9,14 |
| 195 | + |
| 196 | + <?php |
| 197 | + |
| 198 | + namespace App\Http\Controllers; |
| 199 | + |
| 200 | + use App\Contracts\CacheManager; |
| 201 | + |
| 202 | + class CountController extends Controller |
| 203 | + { |
| 204 | + public function __construct( |
| 205 | + private CacheManager $cache, |
| 206 | + ) {} |
| 207 | + |
| 208 | + public function hit(): int |
| 209 | + { |
| 210 | + return $this->cache->increment('counter'); |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + |
| 215 | +Using MongoDB Lock |
| 216 | +------------------ |
| 217 | + |
| 218 | +Atomic locks allow for the manipulation of distributed locks without worrying |
| 219 | +about race conditions. |
| 220 | + |
| 221 | +..code-block:: php |
| 222 | + :emphasize-lines: 3 |
| 223 | + |
| 224 | + use Illuminate\Support\Facades\Cache; |
| 225 | + |
| 226 | + $lock = Cache::store('mongodb')->lock('foo', 10); |
| 227 | + |
| 228 | + if ($lock->get()) { |
| 229 | + // Lock acquired for 10 seconds... |
| 230 | + |
| 231 | + $lock->release(); |
| 232 | + } |
| 233 | + |
| 234 | + |
0 commit comments