Skip to content

Commit 90e5991

Browse files
committed
PHPORM-174 Add doc for cache and locks
1 parent 3de2876 commit 90e5991

File tree

2 files changed

+218
-0
lines changed

2 files changed

+218
-0
lines changed

docs/cache.txt

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
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+
TODO: add reference for all the options
35+
36+
Setup auto-expiration indexes
37+
-----------------------------
38+
39+
The :manual:`TTL indexes </core/index-ttl/>` integrated into MongoDB automatically
40+
delete documents when they have expired. Their use is optional with the ``mongodb``
41+
driver, but recommended as they provide better performance by delegating the
42+
deletion of expired documents to MongoDB instead of requiring the application to
43+
perform this task randomly.
44+
45+
The best way is to create the indexes with a migration calling the methods
46+
``createTTLIndex()`` provided by both the cache and the lock stores:
47+
48+
.. literalinclude:: /includes/cache/migration.php
49+
:language: php
50+
:emphasize-lines: 11,12
51+
:dedent:
52+
53+
Then run the migration:
54+
55+
.. code-block:: none
56+
57+
$ php artisan migrate
58+
59+
Alternatively, the index can be created using :mdb-shell:`MongoDB Shell <>` (``mongosh``):
60+
61+
.. code-block:: ts
62+
63+
db.cache.createIndex(
64+
/* Field that holds the expiration date */
65+
{ expires_at: 1 },
66+
/* Delay to remove items after expiration */
67+
{ expireAfterSeconds: 0 }
68+
)
69+
70+
If you use Locks, disable ``lock_lottery`` by setting the probability to ``0``:
71+
72+
.. code-block:: php
73+
:highlightLines: [4]
74+
75+
'stores' => [
76+
'mongodb' => [
77+
'driver' => 'mongodb',
78+
'connection' => 'mongodb',
79+
'lock_lottery' => [0, 100], // Disabled
80+
],
81+
],
82+
83+
Using MongoDB cache
84+
-------------------
85+
86+
The Laravel cache can be used to store any serializable data using the facade
87+
``Illuminate\Support\Facades\Cache``:
88+
89+
In this example:
90+
- Gets the cache repository with the store ``mongodb``,
91+
- Tries to read and return the cache item named ``foo``,
92+
- If missing, calls the closure to compute the value, store this value forever and return it.
93+
94+
..code-block:: php
95+
96+
use Illuminate\Support\Facades\Cache;
97+
98+
$value = Cache::store('mongodb')->get('foo', function () {
99+
return [1, 2, 3];
100+
});
101+
102+
The default, cached objects do not expire. It is possible to define an expiry time.
103+
104+
..code-block:: php
105+
106+
Cache::store('mongodb')->set('foo', 'abc', '1 day');
107+
108+
Incrementing and decrementing a value is also supported, the value must
109+
be initialized before. The following example initialize the counter to ``3``,
110+
adds 5 and removes 2.
111+
112+
..code-block:: php
113+
114+
Cache::store('mongodb')->set('counter', 3);
115+
Cache::store('mongodb')->increment('counter', 5);
116+
Cache::store('mongodb')->decrement('counter', 2);
117+
118+
.. note::
119+
120+
{+odm-short+} supports incrementing and decrementing with integer and float values.
121+
122+
Configuring MongoDB as default cache
123+
------------------------------------
124+
125+
To use the ``mongodb`` store by default, change the default store in
126+
``config/cache.php``:
127+
128+
.. code-block:: php
129+
:emphasize-lines: 2
130+
131+
return [
132+
'default' => env('CACHE_STORE', 'mongodb'),
133+
134+
'stores' => [
135+
'mongodb' => [
136+
'driver' => 'mongodb',
137+
'connection' => 'mongodb',
138+
],
139+
],
140+
];
141+
142+
The variable ``CACHE_STORE`` could be set in your environment or in
143+
the ``.env`` file. Update or remove it:
144+
145+
.. code-block:: none
146+
147+
CACHE_STORE=mongodb
148+
149+
Then you can use the ``Illuminate\Support\Facades\Cache`` facade and
150+
automatic injection:
151+
152+
.. code-block:: php
153+
154+
use Illuminate\Support\Facades\Cache;
155+
156+
Cache::get('foo', 5);
157+
158+
This example shows how to use automatic injection of the cache
159+
manager, using the default store. It is a controller that
160+
increments a counter each time it is invoked.
161+
162+
163+
.. code-block:: php
164+
:emphasize-lines: 9,14
165+
166+
<?php
167+
168+
namespace App\Http\Controllers;
169+
170+
use App\Contracts\CacheManager;
171+
172+
class CountController extends Controller
173+
{
174+
public function __construct(
175+
private CacheManager $cache,
176+
) {}
177+
178+
public function hit(): int
179+
{
180+
return $this->cache->increment('counter');
181+
}
182+
}
183+
184+
185+
Using MongoDB Lock
186+
------------------
187+
188+
Atomic locks allow for the manipulation of distributed locks without worrying
189+
about race conditions.
190+
191+
..code-block:: php
192+
:emphasize-lines: 3
193+
194+
use Illuminate\Support\Facades\Cache;
195+
196+
$lock = Cache::store('mongodb')->lock('foo', 10);
197+
198+
if ($lock->get()) {
199+
// Lock acquired for 10 seconds...
200+
201+
$lock->release();
202+
}
203+
204+

docs/includes/cache/migration.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Support\Facades\Cache;
5+
6+
return new class extends Migration
7+
{
8+
public function up(): void
9+
{
10+
$store = Cache::store('mongodb');
11+
$store->createTTLIndex();
12+
$store->lock('')->createTTLIndex();
13+
}
14+
};

0 commit comments

Comments
 (0)