@@ -9,11 +9,16 @@ The Cache Component
99===================
1010
1111 The Cache component provides features covering simple to advanced caching needs.
12- It natively implements `PSR-6 `_ and the `Cache Contract `_ for greatest
12+ It natively implements `PSR-6 `_ and the `Cache Contracts `_ for greatest
1313 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.
14+ ready to use adapters for the most common caching backends. It enables tag-based
15+ invalidation and cache stampede protection via locking and early expiration.
16+
17+ .. tip ::
18+
19+ The component also contains adapters to convert between PSR-6, PSR-16 and
20+ Doctrine caches. See :doc: `/components/cache/psr6_psr16_adapters ` and
21+ :doc: `/components/cache/adapters/doctrine_adapter `.
1722
1823Installation
1924------------
@@ -24,8 +29,6 @@ Installation
2429
2530 .. include :: /components/require_autoload.rst.inc
2631
27- .. _cache-psr-6-versus-simple-cache-psr-16 :
28-
2932Cache Contracts versus PSR-6
3033----------------------------
3134
@@ -35,30 +38,24 @@ This component includes *two* different approaches to caching:
3538 A generic cache system, which involves cache "pools" and cache "items".
3639
3740:ref: `Cache Contracts <cache-component-contracts >`:
38- A simple yet powerful way to store, fetch and remove values from a cache .
41+ A simpler yet more powerful way to cache values based on recomputation callbacks .
3942
4043.. tip ::
4144
42- Using the Cache Contracts approach is recommended: using it requires less
45+ Using the Cache Contracts approach is recommended: it requires less
4346 code boilerplate and provides cache stampede protection by default.
4447
45- .. tip ::
46-
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 `.
50-
5148.. _cache-component-contracts :
5249
5350Cache Contracts
5451---------------
5552
56- All adapters supports the Cache Contract. It contains only two methods:
53+ All adapters support the Cache Contracts. They contain only two methods:
5754``get() `` and ``delete() ``. There's no ``set() `` method because the ``get() ``
5855method both gets and sets the cache values.
5956
6057The first thing you need is to instantiate a cache adapter. The
61- :class: `Symfony\\ Component\\ Cache\\ Simple \\ FilesystemCache ` is used in this
58+ :class: `Symfony\\ Component\\ Cache\\ Adapter \\ FilesystemAdapter ` is used in this
6259example::
6360
6461 use Symfony\Component\Cache\Adapter\FilesystemAdapter;
@@ -68,8 +65,8 @@ example::
6865Now you can retrieve and delete cached data using this object. The first
6966argument of the ``get() `` method is a key, an arbitrary string that you
7067associate 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 ::
68+ is a PHP callable which is executed when the key is not found in the cache to
69+ generate and return the value ::
7370
7471 use Symfony\Contracts\Cache\ItemInterface;
7572
@@ -95,20 +92,28 @@ cache) when it's not found in the cache::
9592
9693The Cache Contracts also comes with built in `Stampede prevention `_. This will
9794remove 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
95+ spends 5 seconds to compute data that is cached for 1 hour and this data is accessed
96+ 10 times every second, this means that you mostly have cache hits and everything
97+ is fine. But after 1 hour, we get 10 new requests to a cold cache. So the data
10198is computed again. The next second the same thing happens. So the data is computed
10299about 50 times before the cache is warm again. This is where you need stampede
103100prevention
104101
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".
102+ The first solution is locking: on a per-host basis, only one PHP process is
103+ allowed to compute a specific key at a time. Locking is built in by default so
104+ that you don't have anything specific to do other than leveraging the Cache
105+ Contracts.
106+
107+ The second solution is also built in when using the Cache Contracts: instead of
108+ waiting for the full delay before expiring a value, it is recomputed ahead of its
109+ expiration date: the `Probabilistic early expiration `_ algorithm randomly fakes a
110+ cache miss for one user while others still are served the cached value. You can
111+ control its behavior with the third optional parameter of
112+ :method: `Symfony\\ Contracts\\ Cache\\ CacheInterface::get `,
113+ which is a float value called "beta".
109114
110115By 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
116+ to ``0 `` to disable early recompute and set it to ``INF `` to force an immediate
112117recompute::
113118
114119 use Symfony\Contracts\Cache\ItemInterface;
@@ -126,36 +131,25 @@ Available Cache Adapters
126131
127132The following cache adapters are available:
128133
129- .. tip ::
134+ .. toctree ::
135+ :glob:
136+ :maxdepth: 1
137+
138+ cache/adapters/*
130139
131- To find out more about each of these classes, you can read the
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 `
147140
148141.. _cache-component-psr6-caching :
149142
150- More Generic Caching (PSR-6)
151- ----------------------------
143+ Generic Caching (PSR-6)
144+ -----------------------
152145
153- To use the more- generic, PSR-6 Caching abilities, you'll need to learn its key
146+ To use the generic PSR-6 Caching abilities, you'll need to learn its key
154147concepts:
155148
156149**Item **
157150 A single unit of information stored as a key/value pair, where the key is
158151 the unique identifier of the information and the value is its contents;
152+ see the :doc: `/components/cache/cache_items ` article for more details.
159153**Pool **
160154 A logical repository of cache items. All cache operations (saving items,
161155 looking for items, etc.) are performed through the pool. Applications can
@@ -169,7 +163,7 @@ Basic Usage (PSR-6)
169163-------------------
170164
171165This part of the component is an implementation of `PSR-6 `_, which means that its
172- basic API is the same as defined in the standard . Before starting to cache information,
166+ basic API is the same as defined in the document . Before starting to cache information,
173167create the cache pool using any of the built-in adapters. For example, to create
174168a filesystem-based cache, instantiate :class: `Symfony\\ Component\\ Cache\\ Adapter\\ FilesystemAdapter `::
175169
@@ -199,8 +193,6 @@ Now you can create, retrieve, update and delete items using this cache pool::
199193
200194For a list of all of the supported adapters, see :doc: `/components/cache/cache_pools `.
201195
202- .. _advanced-usage-psr-6 :
203-
204196Advanced Usage
205197--------------
206198
@@ -211,7 +203,8 @@ Advanced Usage
211203 cache/*
212204
213205.. _`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
206+ .. _`Cache Contracts ` : https://github.com/symfony/contracts/blob/master /Cache/CacheInterface.php
215207.. _`PSR-16` : http://www.php-fig.org/psr/psr-16/
216208.. _Doctrine Cache : https://www.doctrine-project.org/projects/cache.html
217209.. _Stampede prevention : https://en.wikipedia.org/wiki/Cache_stampede
210+ .. _Probabilistic early expiration : https://en.wikipedia.org/wiki/Cache_stampede#Probabilistic_early_expiration
0 commit comments