Skip to content

Commit 8b1d3d9

Browse files
Nyholmjaviereguiluz
authored andcommitted
Adding examples of cache contracts
1 parent e7b2bdf commit 8b1d3d9

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

components/cache.rst

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,65 @@ This component includes *two* different approaches to caching:
4646
Doctrine caches. See :doc:`/components/cache/psr6_psr16_adapters` and
4747
:doc:`/components/cache/adapters/doctrine_adapter`.
4848

49+
Cache Contract
50+
--------------
51+
52+
All adapters supports the Cache Contract. It contains only two methods; ``get`` and
53+
``delete``. The first thing you need is to instantiate a cache adapter. The
54+
:class:`Symfony\\Component\\Cache\\Simple\\FilesystemCache` is used in this example::
55+
56+
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
57+
58+
$cache = new FilesystemAdapter();
59+
60+
Now you can retrieve and delete cached data using this object::
61+
62+
use Symfony\Contracts\Cache\ItemInterface;
63+
64+
// The callable will only be executed on a cache miss.
65+
$value = $cache->get('my_cache_key', function (ItemInterface $item) {
66+
$item->expiresAfter(3600);
67+
68+
// ... do some HTTP request or heavy computations
69+
$computedValue = 'foobar';
70+
71+
return $computedValue;
72+
});
73+
74+
echo $value; // 'foobar'
75+
76+
// ... and to remove the cache key
77+
$cache->delete('my_cache_key');
78+
79+
.. note::
80+
81+
Use tags to clear more than one key at the time. Read more at
82+
:doc:`/components/cache/cache_invalidation`.
83+
84+
The Cache Contracts also comes with built in `Stampede prevention`_. This will
85+
remove CPU spikes at the moments when the cache is cold. If an example application
86+
spends 5 seconds to compute data that is cached for 1 hour. This data is accessed
87+
10 times every second. This means that you mostly have cache hits and everything
88+
is fine. But after one hour, we get 10 new requests to a cold cache. So we start
89+
to compute that data again. The next second the same thing happens. So we start
90+
to compute that data about 50 times before the cache is warm again. This is where
91+
you need stampede prevention.
92+
93+
The solution is to recompute the value before the cache expires. The algorithm
94+
randomly fakes a cache miss for one user while others still is served the cached
95+
value. The third parameter to ``CacheInterface::get`` is a beta value. The default
96+
is ``1.0`` which works well in practice. A higher value means earlier recompute.::
97+
98+
use Symfony\Contracts\Cache\ItemInterface;
99+
100+
$beta = 1.0;
101+
$value = $cache->get('my_cache_key', function (ItemInterface $item) {
102+
$item->expiresAfter(3600);
103+
$item->tag(['tag_0', 'tag_1');
104+
105+
return '...';
106+
}, $beta);
107+
49108
Available Cache Adapters
50109
~~~~~~~~~~~~~~~~~~~~~~~~
51110

@@ -134,5 +193,7 @@ Advanced Usage
134193
cache/*
135194

136195
.. _`PSR-6`: http://www.php-fig.org/psr/psr-6/
196+
.. _`Cache Contract`: https://github.com/symfony/contracts/blob/v1.0.0/Cache/CacheInterface.php
137197
.. _`PSR-16`: http://www.php-fig.org/psr/psr-16/
138198
.. _Doctrine Cache: https://www.doctrine-project.org/projects/cache.html
199+
.. _Stampede prevention: https://en.wikipedia.org/wiki/Cache_stampede

0 commit comments

Comments
 (0)