@@ -9,11 +9,16 @@ The Cache Component
9
9
===================
10
10
11
11
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
13
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.
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 `.
17
22
18
23
Installation
19
24
------------
@@ -24,8 +29,6 @@ Installation
24
29
25
30
.. include :: /components/require_autoload.rst.inc
26
31
27
- .. _cache-psr-6-versus-simple-cache-psr-16 :
28
-
29
32
Cache Contracts versus PSR-6
30
33
----------------------------
31
34
@@ -35,25 +38,19 @@ This component includes *two* different approaches to caching:
35
38
A generic cache system, which involves cache "pools" and cache "items".
36
39
37
40
: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 .
39
42
40
43
.. tip ::
41
44
42
- Using the Cache Contracts approach is recommended: using it requires less
45
+ Using the Cache Contracts approach is recommended: it requires less
43
46
code boilerplate and provides cache stampede protection by default.
44
47
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
-
51
48
.. _cache-component-contracts :
52
49
53
50
Cache Contracts
54
51
---------------
55
52
56
- All adapters supports the Cache Contract . It contains only two methods:
53
+ All adapters supports the Cache Contracts . It contains only two methods:
57
54
``get() `` and ``delete() ``. There's no ``set() `` method because the ``get() ``
58
55
method both gets and sets the cache values.
59
56
@@ -68,8 +65,8 @@ example::
68
65
Now you can retrieve and delete cached data using this object. The first
69
66
argument of the ``get() `` method is a key, an arbitrary string that you
70
67
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 ::
68
+ is a PHP callable which is executed when the key is not found in the cache to
69
+ generate and return the value ::
73
70
74
71
use Symfony\Contracts\Cache\ItemInterface;
75
72
@@ -95,20 +92,27 @@ cache) when it's not found in the cache::
95
92
96
93
The Cache Contracts also comes with built in `Stampede prevention `_. This will
97
94
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
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
101
98
is computed again. The next second the same thing happens. So the data is computed
102
99
about 50 times before the cache is warm again. This is where you need stampede
103
100
prevention
104
101
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 that 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 in 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 ``CacheInterface::get() ``,
112
+ which is a float value called "beta".
109
113
110
114
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
115
+ to ``0 `` to disable early recompute and set it to ``INF `` to force an immediate
112
116
recompute::
113
117
114
118
use Symfony\Contracts\Cache\ItemInterface;
@@ -126,36 +130,24 @@ Available Cache Adapters
126
130
127
131
The following cache adapters are available:
128
132
129
- .. tip ::
133
+ .. toctree ::
134
+ :glob:
135
+ :maxdepth: 1
130
136
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 `
137
+ adapters/*
147
138
148
139
.. _cache-component-psr6-caching :
149
140
150
- More Generic Caching (PSR-6)
151
- ----------------------------
141
+ Generic Caching (PSR-6)
142
+ -----------------------
152
143
153
- To use the more- generic, PSR-6 Caching abilities, you'll need to learn its key
144
+ To use the generic PSR-6 Caching abilities, you'll need to learn its key
154
145
concepts:
155
146
156
147
**Item **
157
148
A single unit of information stored as a key/value pair, where the key is
158
149
the unique identifier of the information and the value is its contents;
150
+ see the :doc: `/components/cache/cache_items ` article for more details.
159
151
**Pool **
160
152
A logical repository of cache items. All cache operations (saving items,
161
153
looking for items, etc.) are performed through the pool. Applications can
@@ -169,7 +161,7 @@ Basic Usage (PSR-6)
169
161
-------------------
170
162
171
163
This 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,
164
+ basic API is the same as defined in the document . Before starting to cache information,
173
165
create the cache pool using any of the built-in adapters. For example, to create
174
166
a filesystem-based cache, instantiate :class: `Symfony\\ Component\\ Cache\\ Adapter\\ FilesystemAdapter `::
175
167
@@ -199,8 +191,6 @@ Now you can create, retrieve, update and delete items using this cache pool::
199
191
200
192
For a list of all of the supported adapters, see :doc: `/components/cache/cache_pools `.
201
193
202
- .. _advanced-usage-psr-6 :
203
-
204
194
Advanced Usage
205
195
--------------
206
196
@@ -211,7 +201,8 @@ Advanced Usage
211
201
cache/*
212
202
213
203
.. _`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
204
+ .. _`Cache Contracts ` : https://github.com/symfony/contracts/blob/master /Cache/CacheInterface.php
215
205
.. _`PSR-16` : http://www.php-fig.org/psr/psr-16/
216
206
.. _Doctrine Cache : https://www.doctrine-project.org/projects/cache.html
217
207
.. _Stampede prevention : https://en.wikipedia.org/wiki/Cache_stampede
208
+ .. _Probabilistic early expiration : https://en.wikipedia.org/wiki/Cache_stampede#Probabilistic_early_expiration
0 commit comments