@@ -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
------------
@@ -33,14 +38,19 @@ This component includes *two* different approaches to caching:
33
38
A generic cache system, which involves cache "pools" and cache "items".
34
39
35
40
:ref: `Cache Contracts <cache-component-contracts >`:
36
- 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.
42
+
43
+ .. tip ::
44
+
45
+ Using the Cache Contracts approach is recommended: it requires less
46
+ code boilerplate and provides cache stampede protection by default.
37
47
38
48
.. _cache-component-contracts :
39
49
40
50
Cache Contracts
41
51
---------------
42
52
43
- All adapters supports the Cache Contract . It contains only two methods:
53
+ All adapters supports the Cache Contracts . It contains only two methods:
44
54
``get() `` and ``delete() ``. There's no ``set() `` method because the ``get() ``
45
55
method both gets and sets the cache values.
46
56
@@ -55,8 +65,8 @@ example::
55
65
Now you can retrieve and delete cached data using this object. The first
56
66
argument of the ``get() `` method is a key, an arbitrary string that you
57
67
associate to the cached value so you can retrieve it later. The second argument
58
- is a PHP callable which is executed to generate the value (and store it in the
59
- 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 ::
60
70
61
71
use Symfony\Contracts\Cache\ItemInterface;
62
72
@@ -82,20 +92,27 @@ cache) when it's not found in the cache::
82
92
83
93
The Cache Contracts also comes with built in `Stampede prevention `_. This will
84
94
remove CPU spikes at the moments when the cache is cold. If an example application
85
- spends 5 seconds to compute data that is cached for 1 hour. This data is accessed
86
- 10 times every second. This means that you mostly have cache hits and everything
87
- 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
88
98
is computed again. The next second the same thing happens. So the data is computed
89
99
about 50 times before the cache is warm again. This is where you need stampede
90
100
prevention
91
101
92
- The solution is to recompute the value before the cache expires. The algorithm
93
- randomly fakes a cache miss for one user while others still is served the cached
94
- value. You can control its behavior with the third optional parameter of
95
- ``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".
96
113
97
114
By default the beta is ``1.0 `` and higher values mean earlier recompute. Set it
98
- 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
99
116
recompute::
100
117
101
118
use Symfony\Contracts\Cache\ItemInterface;
@@ -108,52 +125,29 @@ recompute::
108
125
return '...';
109
126
}, $beta);
110
127
111
- .. tip ::
112
-
113
- Using the Cache Contracts approach is recommended: using it requires less
114
- code boilerplate and provides cache stampede protection by default.
115
-
116
- .. tip ::
117
-
118
- The component also contains adapters to convert between PSR-6, PSR-16 and
119
- Doctrine caches. See :doc: `/components/cache/psr6_psr16_adapters ` and
120
- :doc: `/components/cache/adapters/doctrine_adapter `.
121
-
122
128
Available Cache Adapters
123
129
~~~~~~~~~~~~~~~~~~~~~~~~
124
130
125
131
The following cache adapters are available:
126
132
127
- .. tip ::
133
+ .. toctree ::
134
+ :glob:
135
+ :maxdepth: 1
128
136
129
- To find out more about each of these classes, you can read the
130
- :doc: `PSR-6 Cache Pool </components/cache/cache_pools >` page.
131
-
132
- * :class: `Symfony\\ Component\\ Cache\\ Adapter\\ ApcuAdapter `
133
- * :class: `Symfony\\ Component\\ Cache\\ Adapter\\ ArrayAdapter `
134
- * :class: `Symfony\\ Component\\ Cache\\ Adapter\\ ChainAdapter `
135
- * :class: `Symfony\\ Component\\ Cache\\ Adapter\\ DoctrineAdapter `
136
- * :class: `Symfony\\ Component\\ Cache\\ Adapter\\ FilesystemAdapter `
137
- * :class: `Symfony\\ Component\\ Cache\\ Adapter\\ MemcachedAdapter `
138
- * :class: `Symfony\\ Component\\ Cache\\ Adapter\\ NullAdapter `
139
- * :class: `Symfony\\ Component\\ Cache\\ Adapter\\ PdoAdapter `
140
- * :class: `Symfony\\ Component\\ Cache\\ Adapter\\ PhpArrayAdapter `
141
- * :class: `Symfony\\ Component\\ Cache\\ Adapter\\ PhpFilesAdapter `
142
- * :class: `Symfony\\ Component\\ Cache\\ Adapter\\ RedisAdapter `
143
- * :class: `Symfony\\ Component\\ Cache\\ Adapter\\ SimpleCacheAdapter `
144
- * :class: `Symfony\\ Component\\ Cache\\ Adapter\\ TraceableAdapter `
137
+ adapters/*
145
138
146
139
.. _cache-component-psr6-caching :
147
140
148
- More Generic Caching (PSR-6)
149
- ----------------------------
141
+ Generic Caching (PSR-6)
142
+ -----------------------
150
143
151
- 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
152
145
concepts:
153
146
154
147
**Item **
155
148
A single unit of information stored as a key/value pair, where the key is
156
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.
157
151
**Pool **
158
152
A logical repository of cache items. All cache operations (saving items,
159
153
looking for items, etc.) are performed through the pool. Applications can
@@ -167,7 +161,7 @@ Basic Usage (PSR-6)
167
161
-------------------
168
162
169
163
This part of the component is an implementation of `PSR-6 `_, which means that its
170
- 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,
171
165
create the cache pool using any of the built-in adapters. For example, to create
172
166
a filesystem-based cache, instantiate :class: `Symfony\\ Component\\ Cache\\ Adapter\\ FilesystemAdapter `::
173
167
@@ -207,7 +201,8 @@ Advanced Usage
207
201
cache/*
208
202
209
203
.. _`PSR-6` : http://www.php-fig.org/psr/psr-6/
210
- .. _`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
211
205
.. _`PSR-16` : http://www.php-fig.org/psr/psr-16/
212
206
.. _Doctrine Cache : https://www.doctrine-project.org/projects/cache.html
213
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