8
8
The Cache Component
9
9
===================
10
10
11
- The Cache component provides an extended `PSR-6 `_ implementation as well as
12
- a `PSR-16 `_ "Simple Cache" implementation for adding cache to your applications.
13
- It is designed for performance and resiliency, and ships with ready to use
14
- adapters for the most common caching backends, including proxies for adapting
15
- from/to `Doctrine Cache `_.
11
+ The Cache component provides features covering simple to advanced caching needs.
12
+ It natively implements `PSR-6 `_ and the `Cache Contract `_ for greatest
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.
16
17
17
18
Installation
18
19
------------
@@ -23,112 +24,58 @@ Installation
23
24
24
25
.. include :: /components/require_autoload.rst.inc
25
26
26
- Cache (PSR-6) Versus Simple Cache ( PSR-16)
27
- ------------------------------------------
27
+ Cache Contracts versus PSR-6
28
+ ----------------------------
28
29
29
30
This component includes *two * different approaches to caching:
30
31
31
32
:ref: `PSR-6 Caching <cache-component-psr6-caching >`:
32
- A fully-featured cache system, which includes cache "pools", more advanced
33
- cache "items", and :ref: `cache tagging for invalidation <cache-component-tags >`.
33
+ A generic cache system, which involves cache "pools" and cache "items".
34
34
35
- :ref: `PSR-16 Simple Caching <cache-component-psr16-caching >`:
36
- A simple way to store, fetch and remove items from a cache.
37
-
38
- Both methods support the *same * cache adapters and will give you very similar performance.
35
+ :ref: `Cache Contracts <cache-component-contracts >`:
36
+ A simple yet powerful way to store, fetch and remove values from a cache.
39
37
40
38
.. tip ::
41
39
42
- The component also contains adapters to convert between PSR-6 and PSR-16 caches.
43
- See :doc: `/components/cache/psr6_psr16_adapters `.
44
-
45
- .. _cache-component-psr16-caching :
46
-
47
- Simple Caching (PSR-16)
48
- -----------------------
49
-
50
- This part of the component is an implementation of `PSR-16 `_, which means that its
51
- basic API is the same as defined in the standard. First, create a cache object from
52
- one of the built-in cache classes. For example, to create a filesystem-based cache,
53
- instantiate :class: `Symfony\\ Component\\ Cache\\ Simple\\ FilesystemCache `::
54
-
55
- use Symfony\Component\Cache\Simple\FilesystemCache;
56
-
57
- $cache = new FilesystemCache();
40
+ Using the Cache Contracts approach is recommended: using it requires less
41
+ code boilerplate and provides cache stampede protection by default.
58
42
59
- Now you can create, retrieve, update and delete items using this object::
60
-
61
- // save a new item in the cache
62
- $cache->set('stats.products_count', 4711);
63
-
64
- // or set it with a custom ttl
65
- // $cache->set('stats.products_count', 4711, 3600);
66
-
67
- // retrieve the cache item
68
- if (!$cache->has('stats.products_count')) {
69
- // ... item does not exists in the cache
70
- }
71
-
72
- // retrieve the value stored by the item
73
- $productsCount = $cache->get('stats.products_count');
74
-
75
- // or specify a default value, if the key doesn't exist
76
- // $productsCount = $cache->get('stats.products_count', 100);
77
-
78
- // remove the cache key
79
- $cache->delete('stats.products_count');
80
-
81
- // clear *all* cache keys
82
- $cache->clear();
83
-
84
- You can also work with multiple items at once::
85
-
86
- $cache->setMultiple([
87
- 'stats.products_count' => 4711,
88
- 'stats.users_count' => 1356,
89
- ]);
90
-
91
- $stats = $cache->getMultiple([
92
- 'stats.products_count',
93
- 'stats.users_count',
94
- ]);
43
+ .. tip ::
95
44
96
- $cache->deleteMultiple([
97
- 'stats.products_count',
98
- 'stats.users_count',
99
- ]);
45
+ The component also contains adapters to convert between PSR-6, PSR-16 and
46
+ Doctrine caches. See :doc: `/components/cache/psr6_psr16_adapters ` and
47
+ :doc: `/components/cache/adapters/doctrine_adapter `.
100
48
101
- Available Simple Cache (PSR-16) Classes
102
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
49
+ Available Cache Adapters
50
+ ~~~~~~~~~~~~~~~~~~~~~~~~
103
51
104
52
The following cache adapters are available:
105
53
106
54
.. tip ::
107
55
108
56
To find out more about each of these classes, you can read the
109
- :doc: `PSR-6 Cache Pool </components/cache/cache_pools >` page. These "Simple"
110
- (PSR-16) cache classes aren't identical to the PSR-6 Adapters on that page, but
111
- each share constructor arguments and use-cases.
112
-
113
- * :class: `Symfony\\ Component\\ Cache\\ Simple\\ ApcuCache `
114
- * :class: `Symfony\\ Component\\ Cache\\ Simple\\ ArrayCache `
115
- * :class: `Symfony\\ Component\\ Cache\\ Simple\\ ChainCache `
116
- * :class: `Symfony\\ Component\\ Cache\\ Simple\\ DoctrineCache `
117
- * :class: `Symfony\\ Component\\ Cache\\ Simple\\ FilesystemCache `
118
- * :class: `Symfony\\ Component\\ Cache\\ Simple\\ MemcachedCache `
119
- * :class: `Symfony\\ Component\\ Cache\\ Simple\\ NullCache `
120
- * :class: `Symfony\\ Component\\ Cache\\ Simple\\ PdoCache `
121
- * :class: `Symfony\\ Component\\ Cache\\ Simple\\ PhpArrayCache `
122
- * :class: `Symfony\\ Component\\ Cache\\ Simple\\ PhpFilesCache `
123
- * :class: `Symfony\\ Component\\ Cache\\ Simple\\ RedisCache `
124
- * :class: `Symfony\\ Component\\ Cache\\ Simple\\ TraceableCache `
57
+ :doc: `PSR-6 Cache Pool </components/cache/cache_pools >` page.
58
+
59
+ * :class: `Symfony\\ Component\\ Cache\\ Adapter\\ ApcuAdapter `
60
+ * :class: `Symfony\\ Component\\ Cache\\ Adapter\\ ArrayAdapter `
61
+ * :class: `Symfony\\ Component\\ Cache\\ Adapter\\ ChainAdapter `
62
+ * :class: `Symfony\\ Component\\ Cache\\ Adapter\\ DoctrineAdapter `
63
+ * :class: `Symfony\\ Component\\ Cache\\ Adapter\\ FilesystemAdapter `
64
+ * :class: `Symfony\\ Component\\ Cache\\ Adapter\\ MemcachedAdapter `
65
+ * :class: `Symfony\\ Component\\ Cache\\ Adapter\\ NullAdapter `
66
+ * :class: `Symfony\\ Component\\ Cache\\ Adapter\\ PdoAdapter `
67
+ * :class: `Symfony\\ Component\\ Cache\\ Adapter\\ PhpArrayAdapter `
68
+ * :class: `Symfony\\ Component\\ Cache\\ Adapter\\ PhpFilesAdapter `
69
+ * :class: `Symfony\\ Component\\ Cache\\ Adapter\\ RedisAdapter `
70
+ * :class: `Symfony\\ Component\\ Cache\\ Adapter\\ SimpleCacheAdapter `
71
+ * :class: `Symfony\\ Component\\ Cache\\ Adapter\\ TraceableAdapter `
125
72
126
73
.. _cache-component-psr6-caching :
127
74
128
- More Advanced Caching (PSR-6)
129
- -----------------------------
75
+ More Generic Caching (PSR-6)
76
+ ----------------------------
130
77
131
- To use the more-advanced , PSR-6 Caching abilities, you'll need to learn its key
78
+ To use the more-generic , PSR-6 Caching abilities, you'll need to learn its key
132
79
concepts:
133
80
134
81
**Item **
@@ -177,8 +124,8 @@ Now you can create, retrieve, update and delete items using this cache pool::
177
124
178
125
For a list of all of the supported adapters, see :doc: `/components/cache/cache_pools `.
179
126
180
- Advanced Usage (PSR-6)
181
- ----------------------
127
+ Advanced Usage
128
+ --------------
182
129
183
130
.. toctree ::
184
131
:glob:
0 commit comments