Skip to content

Commit e4baf0a

Browse files
committed
Merge branch '4.2'
* 4.2: Update Cache component doc for 4.2
2 parents 56ed618 + 10f8326 commit e4baf0a

File tree

2 files changed

+105
-77
lines changed

2 files changed

+105
-77
lines changed

components/cache.rst

Lines changed: 102 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
The Cache Component
99
===================
1010

11-
The Cache component provides an extended `PSR-6`_ implementation as well as
12-
a `PSR-16`_ "Simple Cache" implementation for adding cache to your applications.
13-
It is designed for performance and resiliency, and ships with ready to use
14-
adapters for the most common caching backends, including proxies for adapting
15-
from/to `Doctrine Cache`_.
11+
The Cache component provides features covering simple to advanced caching needs.
12+
It natively implements `PSR-6`_ and the `Cache Contract`_ for greatest
13+
interoperability. It is designed for performance and resiliency, ships with
14+
ready to use adapters for the most common caching backends, including proxies for
15+
adapting from/to `Doctrine Cache`_ and `PSR-16`_. It enables tag-based invalidation
16+
and cache stampede protection.
1617

1718
Installation
1819
------------
@@ -23,112 +24,133 @@ Installation
2324
2425
.. include:: /components/require_autoload.rst.inc
2526

26-
Cache (PSR-6) Versus Simple Cache (PSR-16)
27-
------------------------------------------
27+
.. _cache-psr-6-versus-simple-cache-psr-16:
28+
29+
Cache Contracts versus PSR-6
30+
----------------------------
2831

2932
This component includes *two* different approaches to caching:
3033

3134
:ref:`PSR-6 Caching <cache-component-psr6-caching>`:
32-
A fully-featured cache system, which includes cache "pools", more advanced
33-
cache "items", and :ref:`cache tagging for invalidation <cache-component-tags>`.
35+
A generic cache system, which involves cache "pools" and cache "items".
36+
37+
:ref:`Cache Contracts <cache-component-contracts>`:
38+
A simple yet powerful way to store, fetch and remove values from a cache.
3439

35-
:ref:`PSR-16 Simple Caching <cache-component-psr16-caching>`:
36-
A simple way to store, fetch and remove items from a cache.
40+
.. tip::
3741

38-
Both methods support the *same* cache adapters and will give you very similar performance.
42+
Using the Cache Contracts approach is recommended: using it requires less
43+
code boilerplate and provides cache stampede protection by default.
3944

4045
.. tip::
4146

42-
The component also contains adapters to convert between PSR-6 and PSR-16 caches.
43-
See :doc:`/components/cache/psr6_psr16_adapters`.
47+
The component also contains adapters to convert between PSR-6, PSR-16 and
48+
Doctrine caches. See :doc:`/components/cache/psr6_psr16_adapters` and
49+
:doc:`/components/cache/adapters/doctrine_adapter`.
4450

45-
.. _cache-component-psr16-caching:
51+
.. _cache-component-contracts:
4652

47-
Simple Caching (PSR-16)
48-
-----------------------
53+
Cache Contracts
54+
---------------
4955

50-
This part of the component is an implementation of `PSR-16`_, which means that its
51-
basic API is the same as defined in the standard. First, create a cache object from
52-
one of the built-in cache classes. For example, to create a filesystem-based cache,
53-
instantiate :class:`Symfony\\Component\\Cache\\Simple\\FilesystemCache`::
56+
All adapters supports the Cache Contract. It contains only two methods:
57+
``get()`` and ``delete()``. There's no ``set()`` method because the ``get()``
58+
method both gets and sets the cache values.
5459

55-
use Symfony\Component\Cache\Simple\FilesystemCache;
60+
The first thing you need is to instantiate a cache adapter. The
61+
:class:`Symfony\\Component\\Cache\\Simple\\FilesystemCache` is used in this
62+
example::
5663

57-
$cache = new FilesystemCache();
64+
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
5865

59-
Now you can create, retrieve, update and delete items using this object::
66+
$cache = new FilesystemAdapter();
6067

61-
// save a new item in the cache
62-
$cache->set('stats.products_count', 4711);
68+
Now you can retrieve and delete cached data using this object. The first
69+
argument of the ``get()`` method is a key, an arbitrary string that you
70+
associate to the cached value so you can retrieve it later. The second argument
71+
is a PHP callable which is executed to generate the value (and store it in the
72+
cache) when it's not found in the cache::
6373

64-
// or set it with a custom ttl
65-
// $cache->set('stats.products_count', 4711, 3600);
74+
use Symfony\Contracts\Cache\ItemInterface;
6675

67-
// retrieve the cache item
68-
if (!$cache->has('stats.products_count')) {
69-
// ... item does not exists in the cache
70-
}
76+
// The callable will only be executed on a cache miss.
77+
$value = $cache->get('my_cache_key', function (ItemInterface $item) {
78+
$item->expiresAfter(3600);
7179

72-
// retrieve the value stored by the item
73-
$productsCount = $cache->get('stats.products_count');
80+
// ... do some HTTP request or heavy computations
81+
$computedValue = 'foobar';
82+
83+
return $computedValue;
84+
});
85+
86+
echo $value; // 'foobar'
7487

75-
// or specify a default value, if the key doesn't exist
76-
// $productsCount = $cache->get('stats.products_count', 100);
88+
// ... and to remove the cache key
89+
$cache->delete('my_cache_key');
7790

78-
// remove the cache key
79-
$cache->delete('stats.products_count');
91+
.. note::
8092

81-
// clear *all* cache keys
82-
$cache->clear();
93+
Use cache tags to delete more than one key at the time. Read more at
94+
:doc:`/components/cache/cache_invalidation`.
8395

84-
You can also work with multiple items at once::
96+
The Cache Contracts also comes with built in `Stampede prevention`_. This will
97+
remove CPU spikes at the moments when the cache is cold. If an example application
98+
spends 5 seconds to compute data that is cached for 1 hour. This data is accessed
99+
10 times every second. This means that you mostly have cache hits and everything
100+
is fine. But after one hour, we get 10 new requests to a cold cache. So the data
101+
is computed again. The next second the same thing happens. So the data is computed
102+
about 50 times before the cache is warm again. This is where you need stampede
103+
prevention
85104

86-
$cache->setMultiple([
87-
'stats.products_count' => 4711,
88-
'stats.users_count' => 1356,
89-
]);
105+
The solution is to recompute the value before the cache expires. The algorithm
106+
randomly fakes a cache miss for one user while others still is served the cached
107+
value. You can control its behavior with the third optional parameter of
108+
``CacheInterface::get()``, which is a float value called "beta".
90109

91-
$stats = $cache->getMultiple([
92-
'stats.products_count',
93-
'stats.users_count',
94-
]);
110+
By default the beta is ``1.0`` and higher values mean earlier recompute. Set it
111+
to ``0`` to disable the recompute and set it to ``INF`` to trigger an immediate
112+
recompute::
95113

96-
$cache->deleteMultiple([
97-
'stats.products_count',
98-
'stats.users_count',
99-
]);
114+
use Symfony\Contracts\Cache\ItemInterface;
100115

101-
Available Simple Cache (PSR-16) Classes
102-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
116+
$beta = 1.0;
117+
$value = $cache->get('my_cache_key', function (ItemInterface $item) {
118+
$item->expiresAfter(3600);
119+
$item->tag(['tag_0', 'tag_1');
120+
121+
return '...';
122+
}, $beta);
123+
124+
Available Cache Adapters
125+
~~~~~~~~~~~~~~~~~~~~~~~~
103126

104127
The following cache adapters are available:
105128

106129
.. tip::
107130

108131
To find out more about each of these classes, you can read the
109-
:doc:`PSR-6 Cache Pool </components/cache/cache_pools>` page. These "Simple"
110-
(PSR-16) cache classes aren't identical to the PSR-6 Adapters on that page, but
111-
each share constructor arguments and use-cases.
112-
113-
* :class:`Symfony\\Component\\Cache\\Simple\\ApcuCache`
114-
* :class:`Symfony\\Component\\Cache\\Simple\\ArrayCache`
115-
* :class:`Symfony\\Component\\Cache\\Simple\\ChainCache`
116-
* :class:`Symfony\\Component\\Cache\\Simple\\DoctrineCache`
117-
* :class:`Symfony\\Component\\Cache\\Simple\\FilesystemCache`
118-
* :class:`Symfony\\Component\\Cache\\Simple\\MemcachedCache`
119-
* :class:`Symfony\\Component\\Cache\\Simple\\NullCache`
120-
* :class:`Symfony\\Component\\Cache\\Simple\\PdoCache`
121-
* :class:`Symfony\\Component\\Cache\\Simple\\PhpArrayCache`
122-
* :class:`Symfony\\Component\\Cache\\Simple\\PhpFilesCache`
123-
* :class:`Symfony\\Component\\Cache\\Simple\\RedisCache`
124-
* :class:`Symfony\\Component\\Cache\\Simple\\TraceableCache`
132+
:doc:`PSR-6 Cache Pool </components/cache/cache_pools>` page.
133+
134+
* :class:`Symfony\\Component\\Cache\\Adapter\\ApcuAdapter`
135+
* :class:`Symfony\\Component\\Cache\\Adapter\\ArrayAdapter`
136+
* :class:`Symfony\\Component\\Cache\\Adapter\\ChainAdapter`
137+
* :class:`Symfony\\Component\\Cache\\Adapter\\DoctrineAdapter`
138+
* :class:`Symfony\\Component\\Cache\\Adapter\\FilesystemAdapter`
139+
* :class:`Symfony\\Component\\Cache\\Adapter\\MemcachedAdapter`
140+
* :class:`Symfony\\Component\\Cache\\Adapter\\NullAdapter`
141+
* :class:`Symfony\\Component\\Cache\\Adapter\\PdoAdapter`
142+
* :class:`Symfony\\Component\\Cache\\Adapter\\PhpArrayAdapter`
143+
* :class:`Symfony\\Component\\Cache\\Adapter\\PhpFilesAdapter`
144+
* :class:`Symfony\\Component\\Cache\\Adapter\\RedisAdapter`
145+
* :class:`Symfony\\Component\\Cache\\Adapter\\SimpleCacheAdapter`
146+
* :class:`Symfony\\Component\\Cache\\Adapter\\TraceableAdapter`
125147

126148
.. _cache-component-psr6-caching:
127149

128-
More Advanced Caching (PSR-6)
129-
-----------------------------
150+
More Generic Caching (PSR-6)
151+
----------------------------
130152

131-
To use the more-advanced, PSR-6 Caching abilities, you'll need to learn its key
153+
To use the more-generic, PSR-6 Caching abilities, you'll need to learn its key
132154
concepts:
133155

134156
**Item**
@@ -177,8 +199,10 @@ Now you can create, retrieve, update and delete items using this cache pool::
177199

178200
For a list of all of the supported adapters, see :doc:`/components/cache/cache_pools`.
179201

180-
Advanced Usage (PSR-6)
181-
----------------------
202+
.. _advanced-usage-psr-6:
203+
204+
Advanced Usage
205+
--------------
182206

183207
.. toctree::
184208
:glob:
@@ -187,5 +211,7 @@ Advanced Usage (PSR-6)
187211
cache/*
188212

189213
.. _`PSR-6`: http://www.php-fig.org/psr/psr-6/
214+
.. _`Cache Contract`: https://github.com/symfony/contracts/blob/v1.0.0/Cache/CacheInterface.php
190215
.. _`PSR-16`: http://www.php-fig.org/psr/psr-16/
191216
.. _Doctrine Cache: https://www.doctrine-project.org/projects/cache.html
217+
.. _Stampede prevention: https://en.wikipedia.org/wiki/Cache_stampede

components/cache/psr6_psr16_adapters.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
Adapters For Interoperability between PSR-6 and PSR-16 Cache
77
============================================================
88

9-
Sometimes, you may have a Cache object that implements the :ref:`PSR-16 <cache-component-psr16-caching>`
9+
Sometimes, you may have a Cache object that implements the `PSR-16`_
1010
standard, but need to pass it to an object that expects a :ref:`PSR-6 <cache-component-psr6-caching>`
1111
cache adapter. Or, you might have the opposite situation. The cache component contains
1212
two classes for bidirectional interoperability between PSR-6 and PSR-16 caches.
@@ -84,3 +84,5 @@ this use-case::
8484

8585
// now use this wherever you want
8686
$githubApiClient = new GitHubApiClient($psr16Cache);
87+
88+
.. _`PSR-16`: http://www.php-fig.org/psr/psr-16/

0 commit comments

Comments
 (0)