Skip to content

Commit e5c28d7

Browse files
committed
[#11329] Readded the Symfony cache contracts example
1 parent 00084d5 commit e5c28d7

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

cache.rst

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,29 @@ developed for high performance.
1010

1111
Basic uses of the cache looks like this::
1212

13-
if (!$cache->has('my_cache_key')) {
13+
use Symfony\Contracts\Cache\ItemInterface;
14+
15+
// The callable will only be executed on a cache miss.
16+
$value = $pool->get('my_cache_key', function (ItemInterface $item) {
17+
$item->expiresAfter(3600);
18+
1419
// ... do some HTTP request or heavy computations
15-
$cache->set('my_cache_key', 'foobar', 3600);
16-
}
20+
$computedValue = 'foobar';
1721

18-
echo $cache->get('my_cache_key'); // 'foobar'
22+
return $computedValue;
23+
});
1924

20-
// ... and to remove the cache key
21-
$cache->delete('my_cache_key');
25+
echo $value; // 'foobar'
2226

27+
// ... and to remove the cache key
28+
$pool->delete('my_cache_key');
2329

2430
Symfony supports PSR-6 and PSR-16 cache interfaces. You can read more about
2531
these at the :doc:`component documentation </components/cache>`.
2632

33+
.. versionadded:: 4.2
34+
35+
The cache contracts were introduced in Symfony 4.2.
2736

2837
Configuring Cache with FrameworkBundle
2938
--------------------------------------

0 commit comments

Comments
 (0)