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,113 @@ 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 <cache-component-tags >`.
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
+ .. tip ::
43
+
44
+ The component also contains adapters to convert between PSR-6 and PSR-16 caches.
45
+ See :doc: `/components/cache/psr6_psr16_adapters `.
46
+
47
+ .. _cache-component-psr16-caching :
48
+
49
+ Simple Caching (PSR-16)
50
+ -----------------------
51
+
52
+ This part of the component is an implementation of `PSR-16 `_, which means that its
53
+ basic API is the same as defined in the standard. First, create a cache object from
54
+ one of the built-in cache classes. For example, to create a filesystem-based cache,
55
+ instantiate :class: `Symfony\\ Component\\ Cache\\ Simple\\ FilesystemCache `::
56
+
57
+ use Symfony\Component\Cache\Simple\FilesystemCache;
58
+
59
+ $cache = new FilesystemCache();
60
+
61
+ Now you can create, retrieve, update and delete items using this object::
62
+
63
+ // save a new item in the cache
64
+ $cache->set('stats.num_products', 4711);
65
+
66
+ // or set it with a custom ttl
67
+ // $cache->set('stats.num_products', 4711, 3600);
68
+
69
+ // retrieve the cache item
70
+ if (!$cache->has('stats.num_products')) {
71
+ // ... item does not exists in the cache
72
+ }
73
+
74
+ // retrieve the value stored by the item
75
+ $numProducts = $cache->get('stats.num_products');
76
+
77
+ // or specify a default value, if the key doesn't exist
78
+ // $numProducts = $cache->get('stats.num_products', 100);
79
+
80
+ // remove the cache key
81
+ $cache->delete('stats.num_products');
82
+
83
+ // clear *all* cache keys
84
+ $cache->clear();
85
+
86
+ You can also work with multiple items at once::
87
+
88
+ $cache->setMultiple(array(
89
+ 'stats.num_products' => 4711,
90
+ 'stats.num_users' => 1356,
91
+ ));
92
+
93
+ $stats = $cache->getMultiple(array(
94
+ 'stats.num_products',
95
+ 'stats.num_users',
96
+ ));
26
97
27
- Before starting to use the Cache component, it's important that you learn the
28
- meaning of some key concepts:
98
+ $cache->deleteMultiple(array(
99
+ 'stats.num_products',
100
+ 'stats.num_users',
101
+ ));
102
+
103
+ Available Simple Cache (PSR-16) Classes
104
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
105
+
106
+ The following cache adapters are available:
107
+
108
+ .. tip ::
109
+
110
+ To find out more about each of these classes, you can read the
111
+ :doc: `PSR-6 Cache Pool </components/cache/cache_pools >` page. These "Simple"
112
+ (PSR-16) cache classes aren't identical to the PSR-6 Adapters on that page, but
113
+ each share constructor arguments and use-cases.
114
+
115
+ * :class: `Symfony\\ Component\\ Cache\\ Simple\\ ApcuCache `
116
+ * :class: `Symfony\\ Component\\ Cache\\ Simple\\ ArrayCache `
117
+ * :class: `Symfony\\ Component\\ Cache\\ Simple\\ ChainCache `
118
+ * :class: `Symfony\\ Component\\ Cache\\ Simple\\ DoctrineCache `
119
+ * :class: `Symfony\\ Component\\ Cache\\ Simple\\ FilesystemCache `
120
+ * :class: `Symfony\\ Component\\ Cache\\ Simple\\ MemcachedCache `
121
+ * :class: `Symfony\\ Component\\ Cache\\ Simple\\ NullCache `
122
+ * :class: `Symfony\\ Component\\ Cache\\ Simple\\ PdoCache `
123
+ * :class: `Symfony\\ Component\\ Cache\\ Simple\\ PhpArrayCache `
124
+ * :class: `Symfony\\ Component\\ Cache\\ Simple\\ PhpFilesCache `
125
+ * :class: `Symfony\\ Component\\ Cache\\ Simple\\ RedisCache `
126
+ * :class: `Symfony\\ Component\\ Cache\\ Simple\\ TraceableCache `
127
+
128
+ .. _cache-component-psr6-caching :
129
+
130
+ More Advanced Caching (PSR-6)
131
+ -----------------------------
132
+
133
+ To use the more-advanced, PSR-6 Caching abilities, you'll need to learn its key
134
+ concepts:
29
135
30
136
**Item **
31
137
A single unit of information stored as a key/value pair, where the key is
@@ -39,11 +145,11 @@ meaning of some key concepts:
39
145
filesystem, in a database, etc. The component provides several ready to use
40
146
adapters for common caching backends (Redis, APCu, etc.)
41
147
42
- Basic Usage
43
- -----------
148
+ Basic Usage (PSR-6)
149
+ -------------------
44
150
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,
151
+ This part of the component is an implementation of `PSR-6 `_, which means that its
152
+ basic API is the same as defined in the standard. Before starting to cache information,
47
153
create the cache pool using any of the built-in adapters. For example, to create
48
154
a filesystem-based cache, instantiate :class: `Symfony\\ Component\\ Cache\\ Adapter\\ FilesystemAdapter `::
49
155
@@ -71,8 +177,10 @@ Now you can create, retrieve, update and delete items using this cache pool::
71
177
// remove the cache item
72
178
$cache->deleteItem('stats.num_products');
73
179
74
- Advanced Usage
75
- --------------
180
+ For a list of all of the supported adapters, see :doc: `/components/cache/cache_pools `.
181
+
182
+ Advanced Usage (PSR-6)
183
+ ----------------------
76
184
77
185
.. toctree ::
78
186
:glob:
@@ -81,4 +189,5 @@ Advanced Usage
81
189
cache/*
82
190
83
191
.. _`PSR-6` : http://www.php-fig.org/psr/psr-6/
192
+ .. _`PSR-16` : http://www.php-fig.org/psr/psr-16/
84
193
.. _Packagist : https://packagist.org/packages/symfony/cache
0 commit comments