You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: developer_manual/basics/caching.rst
+38Lines changed: 38 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -59,6 +59,44 @@ The ``OCP\Cache\CappedMemoryCache`` class is an in-memory cache that can be used
59
59
}
60
60
}
61
61
62
+
63
+
A cache instance can also be built by the cache factory. This approach allows mocking the injected factory for finer control during :ref:`unit testing<testing-php>`:
64
+
65
+
.. code-block:: php
66
+
:caption: lib/Service/PictureService.php
67
+
:emphasize-lines: 15-19
68
+
69
+
<?php
70
+
71
+
namespace OCA\MyApp\Service;
72
+
73
+
use OCP\ICacheFactory;
74
+
75
+
class PictureService {
76
+
private ICacheFactory $cacheFactory;
77
+
78
+
public function __construct(ICacheFactory $cacheFactory){
79
+
$this->cacheFactory = $cacheFactory;
80
+
}
81
+
82
+
public function getPicture(string $url): void {
83
+
// Initialize the cache. The instance has to be remembered because
84
+
// each call to `createInMemory` returns a fresh, empty cache.
0 commit comments