6
6
The Cache Component
7
7
===================
8
8
9
- The Cache component provides an extended `PSR-6 `_ implementation for adding
10
- cache to your applications. It is designed to have a low overhead and it
11
- ships with ready to use adapters for the most common caching backends.
9
+ The Cache component provides an extended `PSR-6 `_ implementation as well as
10
+ a `PSR-16 `_ "Simple Cache" implementation for adding cache to your applications.
11
+ It is designed to have a low overhead and it ships with ready to use adapters
12
+ for the most common caching backends.
12
13
13
14
.. versionadded :: 3.1
14
15
The Cache component was introduced in Symfony 3.1.
15
16
17
+ .. versionadded :: 3.3
18
+ The PSR-16 "Simple Cache" implementation was introduced in Symfony 3.3.
19
+
16
20
Installation
17
21
------------
18
22
@@ -21,11 +25,27 @@ You can install the component in 2 different ways:
21
25
* :doc: `Install it via Composer </components/using_components >` (``symfony/cache `` on `Packagist `_);
22
26
* Use the official Git repository (https://github.com/symfony/cache).
23
27
24
- Key Concepts
25
- ------------
28
+ Cache (PSR-6) Versus Simple Cache (PSR-16)
29
+ ------------------------------------------
30
+
31
+ This component includes *two * different approaches to caching:
32
+
33
+ :ref: `PSR-6 Caching <cache-component-psr6-caching >`:
34
+ A fully-featured cache system, which includes cache "pools", more advanced
35
+ cache "items", and :ref: `cache tagging for invalidation <TODO >`.
36
+
37
+ :ref: `PSR-16 Simple Caching <cache-component-psr16-caching >`:
38
+ A simple way to store, fetch and remove items from a cache.
39
+
40
+ Both methods support the *same * cache adapters and will give you very similar performance.
41
+
42
+ .. _cache-component-psr6-caching :
26
43
27
- Before starting to use the Cache component, it's important that you learn the
28
- meaning of some key concepts:
44
+ More Advanced Caching (PSR-6)
45
+ -----------------------------
46
+
47
+ To use the more-advanced, PSR-6 Caching abilities, you'll need to learn its key
48
+ concepts:
29
49
30
50
**Item **
31
51
A single unit of information stored as a key/value pair, where the key is
@@ -39,11 +59,11 @@ meaning of some key concepts:
39
59
filesystem, in a database, etc. The component provides several ready to use
40
60
adapters for common caching backends (Redis, APCu, etc.)
41
61
42
- Basic Usage
43
- -----------
62
+ Basic Usage (PSR-6)
63
+ -------------------
44
64
45
- This component is an implementation of `PSR-6 `_, which means that its basic API
46
- is the same as defined in the standard. Before starting to cache information,
65
+ This part of the component is an implementation of `PSR-6 `_, which means that its
66
+ basic API is the same as defined in the standard. Before starting to cache information,
47
67
create the cache pool using any of the built-in adapters. For example, to create
48
68
a filesystem-based cache, instantiate :class: `Symfony\\ Component\\ Cache\\ Adapter\\ FilesystemAdapter `::
49
69
@@ -71,14 +91,98 @@ Now you can create, retrieve, update and delete items using this cache pool::
71
91
// remove the cache item
72
92
$cache->deleteItem('stats.num_products');
73
93
74
- Advanced Usage
75
- --------------
94
+ For a list of all of the supported adapters, see :doc: `/components/cache/cache_pools `.
95
+
96
+ Advanced Usage (PSR-6)
97
+ ----------------------
76
98
77
99
.. toctree ::
78
100
:glob:
79
101
:maxdepth: 1
80
102
81
103
cache/*
82
104
105
+ .. _cache-component-psr16-caching :
106
+
107
+ Simple Caching (PSR-16)
108
+ -----------------------
109
+
110
+ This part of the component is an implementation of `PSR-16 `_, which means that its
111
+ basic API is the same as defined in the standard. First, create a cache object from
112
+ one of the built-in cache clases. For example, to create a filesystem-based cache,
113
+ instantiate :class: `Symfony\\ Component\\ Cache\\ Simple\\ FilesystemCache `::
114
+
115
+ use Symfony\Component\Cache\Simple\FilesystemCache;
116
+
117
+ $cache = new FilesystemCache();
118
+
119
+ Now you can create, retrieve, update and delete items using this object::
120
+
121
+ // save a new item in the cache
122
+ $cache->set('stats.num_products', 4711);
123
+
124
+ // or set it with a custom ttl
125
+ // $cache->set('stats.num_products', 4711, 3600);
126
+
127
+ // retrieve the cache item
128
+ if (!$cache->has('stats.num_products')) {
129
+ // ... item does not exists in the cache
130
+ }
131
+
132
+ // retrieve the value stored by the item
133
+ $numProducts = $cache->get('stats.num_products');
134
+
135
+ // or specify a default value, if the key doesn't exist
136
+ // $numProducts = $cache->get('stats.num_products', 100);
137
+
138
+ // remove the cache key
139
+ $cache->deleteItem('stats.num_products');
140
+
141
+ // clear *all* cache keys
142
+ $cache->clear();
143
+
144
+ You can also work with multiple items at once::
145
+
146
+ $cache->setMultiple([
147
+ 'stats.num_products' => 4711,
148
+ 'stats.num_users' => 1356,
149
+ ]);
150
+
151
+ $stats = $cache->getMultiple([
152
+ 'stats.num_products',
153
+ 'stats.num_users',
154
+ ]);
155
+
156
+ $cache->deleteMultiple([
157
+ 'stats.num_products',
158
+ 'stats.num_users',
159
+ ]);
160
+
161
+ Available Simple Cache (PSR-16) Classes
162
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
163
+
164
+ The following cache adapters are available:
165
+
166
+ .. tip ::
167
+
168
+ To find out more about each of these classes, you can read th
169
+ :doc: `PSR-6 Cache Pool </components/cache/cache_pools >` page. These "Simple"
170
+ (PSR-16) cache classes aren't identical to the PSR-6 Adapters on that page, but
171
+ each share constructor arguments and use-cases.
172
+
173
+ * :class: `Symfony\\ Component\\ Cache\\ Simple\\ ApcuCache `
174
+ * :class: `Symfony\\ Component\\ Cache\\ Simple\\ ArrayCache `
175
+ * :class: `Symfony\\ Component\\ Cache\\ Simple\\ ChainCache `
176
+ * :class: `Symfony\\ Component\\ Cache\\ Simple\\ DoctrineCache `
177
+ * :class: `Symfony\\ Component\\ Cache\\ Simple\\ FilesystemCache `
178
+ * :class: `Symfony\\ Component\\ Cache\\ Simple\\ MemcachedCache `
179
+ * :class: `Symfony\\ Component\\ Cache\\ Simple\\ NullCache `
180
+ * :class: `Symfony\\ Component\\ Cache\\ Simple\\ PdoCache `
181
+ * :class: `Symfony\\ Component\\ Cache\\ Simple\\ PhpArrayCache `
182
+ * :class: `Symfony\\ Component\\ Cache\\ Simple\\ PhpFilesCache `
183
+ * :class: `Symfony\\ Component\\ Cache\\ Simple\\ RedisCache `
184
+ * :class: `Symfony\\ Component\\ Cache\\ Simple\\ TraceableCache `
185
+
83
186
.. _`PSR-6` : http://www.php-fig.org/psr/psr-6/
187
+ .. _`PSR-16` : http://www.php-fig.org/psr/psr-16/
84
188
.. _Packagist : https://packagist.org/packages/symfony/cache
0 commit comments