@@ -5,9 +5,7 @@ Using cache is a great way of making you application run quicker. The Symfony ca
5
5
component is shipped with many adapters to different storages. Every adapter is
6
6
developed for high performance.
7
7
8
- Basic uses of the cache looks like this.
9
-
10
- .. code-block :: php
8
+ Basic uses of the cache looks like this::
11
9
12
10
use Symfony\Contracts\Cache\ItemInterface;
13
11
@@ -27,11 +25,11 @@ Basic uses of the cache looks like this.
27
25
28
26
29
27
Symfony also supports PSR-6 and PSR-16 cache interfaces. You can read more about
30
- these at the :ref : `component documentation <component- cache >`.
28
+ these at the :doc : `component documentation </components/ cache >`.
31
29
32
30
33
- Configuring Cache with SymfonyFrameworkBundle
34
- ---------------------------------------------
31
+ Configuring Cache with FrameworkBundle
32
+ --------------------------------------
35
33
36
34
When configuring the the cache component there are a few concepts you should know
37
35
of:
46
44
47
45
There are two pools that are always enabled by default. They are ``cache.app `` and
48
46
``cache.system ``. The system cache is use for things like annotations, serializer,
49
- and validation. The ``` cache.app `` is used in your code. You can configure which
47
+ and validation. The ``cache.app `` can be used in your code. You can configure which
50
48
adapter (template) they use by using the ``app `` and ``system `` key like:
51
49
52
50
.. code-block :: yaml
53
51
52
+ # config/packages/cache.yaml
54
53
framework :
55
54
cache :
56
55
app : cache.adapter.filesystem
57
56
system : cache.adapter.system
58
57
59
58
The Cache component comes with a series of adapters already created:
60
59
61
- - :ref: `cache.adapter.apcu <component-cache-apcu-adapter >`
62
- - :ref: `ChainAdapter <component-cache-chain-adapter >`
63
- - :ref: `cache.adapter.doctrine <component-cache-doctrine-adapter >`
64
- - :ref: `cache.adapter.filesystem <component-cache-filesystem-adapter >`
65
- - :ref: `cache.adapter.memcached <component-cache-memcached-adapter >`
66
- - :ref: `cache.adapter.pdo <component-cache-pdo-doctrine-dbal-adapter >`
67
- - :ref: `PHPArrayAdapter <component-cache-php-array-adapter >`
68
- - :ref: `PHPFileAdapter <component-cache-php-files-adapter >`
69
- - :ref: `ProxyAdapter <component-cache-proxy-adapter >`
70
- - :ref: `cache.adapter.redis <component-cache-redis-adapter >`
71
- - cache.adapter.psr6
72
- - cache.adapter.system
73
- - NullAdapter
74
- - cache.adapter.array
75
-
76
- Some of these adapters could be configured via shortcuts:
60
+ * :doc: `cache.adapter.apcu </components/cache/adapters/apcu-adapter >`
61
+ * :doc: `cache.adapter.array </components/cache/adapters/array-cache-adapter >`
62
+ * :doc: `cache.adapter.doctrine </components/cache/adapters/doctrine-adapter >`
63
+ * :doc: `cache.adapter.filesystem </components/cache/adapters/filesystem-adapter >`
64
+ * :doc: `cache.adapter.memcached </components/cache/adapters/memcached-adapter >`
65
+ * :doc: `cache.adapter.pdo </components/cache/adapters/pdo-doctrine-dbal-adapter >`
66
+ * :doc: `cache.adapter.redis </components/cache/adapters/redis-adapter >`
67
+ * :doc: `PHPFileAdapter </components/cache/adapters/php-files-adapter >`
68
+ * :doc: `PHPArrayAdapter </components/cache/adapters/php-array-adapter >`
69
+
70
+ * :doc: `ChainAdapter </components/cache/adapters/chain-adapter >`
71
+ * :doc: `ProxyAdapter </components/cache/adapters/proxy-adapter >`
72
+ * cache.adapter.psr6
73
+
74
+ * cache.adapter.system
75
+ * NullAdapter
76
+
77
+ Some of these adapters could be configured via shortcuts. Using these shortcuts
78
+ will create pool with service id of ``cache.[type] ``
77
79
78
80
.. code-block :: yaml
79
81
82
+ # config/packages/cache.yaml
80
83
framework :
81
84
cache :
82
85
directory : ' %kernel.cache_dir%/pools' # Only used with cache.adapter.filesystem
83
86
87
+ # service: cache.doctrine
84
88
default_doctrine_provider : ' app.doctrine_cache'
89
+ # service: cache.psr6
85
90
default_psr6_provider : ' app.my_psr6_service'
91
+ # service: cache.redis
86
92
default_redis_provider : ' redis://localhost'
93
+ # service: cache.memcached
87
94
default_memcached_provider : ' memcached://localhost'
95
+ # service: cache.pdo
88
96
default_pdo_provider : ' doctrine.dbal.default_connection'
89
97
90
- Using the *providers * above, they will configure the adapters and create pools (services)
91
- with service ids:
92
-
93
- - ``cache.doctrine ``
94
- - ``cache.psr6 ``
95
- - ``cache.redis ``
96
- - ``cache.memcached ``
97
- - ``cache.pdo ``
98
-
99
98
Creating Custom Pools
100
99
---------------------
101
100
102
- You can also create more customized pools. All you need is an adapter
101
+ You can also create more customized pools. All you need is an adapter:
103
102
104
103
.. code-block :: yaml
105
104
105
+ # config/packages/cache.yaml
106
106
framework :
107
107
cache :
108
108
default_memcached_provider : ' memcached://localhost'
@@ -117,14 +117,15 @@ You can also create more customized pools. All you need is an adapter
117
117
118
118
The configuration above will create 3 services: ``my_cache_pool ``, ``cache.acme ``
119
119
and ``cache.foobar ``. The ``my_cache_pool `` pool is using the ArrayAdapter
120
- and the other two are using the :ref : `MemcachedAdapter <component- cache- memcached-adapter >`.
120
+ and the other two are using the :doc : `MemcachedAdapter </components/ cache/adapters/ memcached-adapter >`.
121
121
The ``cache.acme `` pool is using the Memcached server on localhost and ``cache.foobar ``
122
122
is using the Memcached server at example.com.
123
123
124
124
For advanced configurations it could sometimes be useful to use a pool as an adapter.
125
125
126
126
.. code-block :: yaml
127
127
128
+ # config/packages/cache.yaml
128
129
framework :
129
130
cache :
130
131
pools :
@@ -145,17 +146,19 @@ Different cache adapters has different strengths and weaknesses. Some might be r
145
146
quick but small and some may be able to contain a lot of data but are quite slow.
146
147
To get the best of both worlds you may use a chain of adapters. The idea is to
147
148
first look at the quick adapter and then move on to slower adapters. In the worst
148
- case we have to recalculate the value .
149
+ case the value needs to be recalculated .
149
150
150
151
.. code-block :: yaml
151
152
153
+ # config/services.yaml
152
154
services :
153
155
app.my_cache_chain_adapter :
154
156
class : Symfony\Component\Cache\Adapter\ChainAdapter
155
157
arguments :
156
158
- ['cache.adapter.array', 'cache.my_redis', 'cache.adapter.file']
157
159
- 31536000 # One year
158
160
161
+ # config/packages/cache.yaml
159
162
framework :
160
163
cache :
161
164
pools :
@@ -189,4 +192,4 @@ clearer.
189
192
190
193
.. code-block :: terminal
191
194
192
- $ bin/console cache:pool:clear cache.app_clearer
195
+ $ php bin/console cache:pool:clear cache.app_clearer
0 commit comments